2016-07-25 21 views
1
[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;[email protected]"); 
    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是我的標籤,我想它設置爲可見的,如果我的條件不滿足

+0

使用java腳本可見Lable成功後web方法 –

+0

如何使用,你可以告訴我一個演示請 –

+0

你的代碼返回一個字符串列表,這個方法不是一個webforms類型的調用。 – Steve

回答

0

使用此示例

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"; 
      } 
     }); 

    } 
+3

如何使用jQuery進行調用,但不隱藏DOM的一部分,這很有趣。 –

+0

其甚至不工作 –

+0

附上您的代碼PLZ –

0
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(); 
     } 
    } 
} 
+0

標籤如果失敗可見 –

0
** now use this code:** 
    Label lbl = new Label(); 
    lbl.Attributes.Add("ID", "id"); 
    lbl.Text = ""; 
     if (false) 
     { 
      lbl.Text = "your Text"; 
     } 
     else 
     { 
      lbl.Text = ""; 
     } 
+0

在page.findcontrol上顯示錯誤(名稱頁面不存在於當前上下文中) –

+0

使用(Label)this.Page而不是(Label)頁面 – Rayudu

+0

關鍵字在靜態屬性中無效 –

0

的WebMethod是web服務內,你不應該訪問其內部控制。這兩者完全是分開的目的。不管是什麼情況,你都應該檢查你的網頁並相應地調用你的Web服務。混合網頁相關的代碼,如訪問Web服務內的控件是真的很糟糕的設計。