在webmethod c#中设置标签的可见性
本文关键字:标签 可见性 设置 webmethod | 更新日期: 2023-09-27 17:58:37
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> lfromp(string id)
{
if (HttpContext.Current != null)
{
Page page = (Page)HttpContext.Current.Handler;
Label lbltxt = (Label)page.FindControl("lbltxt");
}
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=logistics.jayom.org,1434;Initial Catalog=logistics_kl;Persist Security Info=True;User ID=kl_admin;Password=Admin@2222");
con.Open();
SqlCommand cmd = new SqlCommand("SPlgfpro", con); //select login from profile
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param;
param = new SqlParameter("@id", id);
param.DbType = DbType.String;
param.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//dlstprofile.Items.Clear();
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// dlstprofile.Items.Add(ds.Tables[0].Rows[i][0].ToString());
//}
con.Close();
SqlCommand lfp = new SqlCommand("SPlgfpro");//select class from class
lfp.CommandType = CommandType.StoredProcedure;
lfp.Connection = con;
SqlParameter dpra;
dpra = new SqlParameter("@id", id);
dpra.Direction = ParameterDirection.Input;
dpra.DbType = DbType.String;
lfp.Parameters.Add(dpra);
con.Open();
lfp.ExecuteNonQuery();
SqlDataAdapter lda1 = new SqlDataAdapter(lfp);
DataSet dds1 = new DataSet();
lda1.Fill(dds1);
SqlDataReader drlp = lfp.ExecuteReader();
{
if (drlp.Read())
{
id = drlp["login"].ToString();
}
else
{
//if (HttpContext.Current != null)
//{
// Page page = (Page)HttpContext.Current.Handler;
// Label lbltxt = (Label)page.FindControl("lbltxt");
// lbltxt.Visible = true;
//}
}
con.Close();
}
List<string> emp = new List<string>();
return emp;
}
这是我的cs
页面,如果我没有使用标签,但我不能在中使用标签,代码运行正常
[网络方法]
这里lbltxt
是我的标签,如果我的条件不满足,我想将其设置为可见。
使用此示例
function RunWebMethod() {
$.ajax({
type: "POST",
url: "Test.aspx/RunWebMethod",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (msg) {
document.getElementById("LableName").style.visibility = "hidden";
}
});
}
static Label lbl=null;
protected void Page_Load(object sender, EventArgs e)
{
lbl = (Label)this.Page.FindControl("lbltxt");
}
[WebMethod]
public static List<string> lfromp(string id)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("MYCONNECTIONSTRING");
con.Open();
SqlCommand cmd = new SqlCommand("SPlgfpro", con); //select login from profile
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param;
param = new SqlParameter("@id", id);
param.DbType = DbType.String;
param.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//dlstprofile.Items.Clear();
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// dlstprofile.Items.Add(ds.Tables[0].Rows[i][0].ToString());
//}
con.Close();
SqlCommand lfp = new SqlCommand("SPlgfpro");//select class from class
lfp.CommandType = CommandType.StoredProcedure;
lfp.Connection = con;
SqlParameter dpra;
dpra = new SqlParameter("@id", id);
dpra.Direction = ParameterDirection.Input;
dpra.DbType = DbType.String;
lfp.Parameters.Add(dpra);
con.Open();
lfp.ExecuteNonQuery();
SqlDataAdapter lda1 = new SqlDataAdapter(lfp);
DataSet dds1 = new DataSet();
lda1.Fill(dds1);
SqlDataReader drlp = lfp.ExecuteReader();
{
if (drlp.Read())
{
id = drlp["login"].ToString();
}
else
{
Label l = lbl;
l.Visible = true;
}
}
con.Close();
}
}
}
WebMethod在Web服务中,您不应该访问其中的控件。这两者是完全独立的东西,用于不同的目的。无论情况如何,你都应该在你的网页上查看并相应地调用你的网络服务。混合使用与网页相关的代码(如访问web服务中的控件(是非常糟糕的设计。
** now use this code:**
Label lbl = new Label();
lbl.Attributes.Add("ID", "id");
lbl.Text = "";
if (false)
{
lbl.Text = "your Text";
}
else
{
lbl.Text = "";
}