![](/img/trans.png)
[英]C# Set the visibility of a label using the result of String.IsNullOrEmpty
[英]set visibility of label inside webmethod c#
[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
頁面,如果我未使用標簽,但是無法在我的標簽中使用代碼,則代碼可以正常運行
[WEBMETHOD]
這里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();
}
}
}
** now use this code:**
Label lbl = new Label();
lbl.Attributes.Add("ID", "id");
lbl.Text = "";
if (false)
{
lbl.Text = "your Text";
}
else
{
lbl.Text = "";
}
WebMethod位於Web服務內部,您不應訪問其中的控件。 出於完全不同的目的,兩者是完全分開的東西。 無論情況如何,都應在網頁上檢查並相應地調用Web服務。 混合與網頁相關的代碼(例如訪問Web服務中的控件)確實是一種糟糕的設計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.