c#如何在静态Web方法中获取asp.net文本框值?

时间:2015-08-22 06:49:59

标签: javascript c# asp.net

我想从JavaScript计时器中调用c#方法,如下所示。

<script type="text/javascript">

     window.setInterval(DeleteKartItems, 10000);

     function DeleteKartItems() {
            PageMethods.DeleteItem();
            alert("test");
     }
</script>

c#方法

public static void DeleteItem()
{

     string query = "[Get_Messages]";
     SqlCommand cmd = new SqlCommand(query);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@roomId", 5);
     GetData(cmd);
}

private static void GetData(SqlCommand cmd)
{
     string strConnString = ConfigurationManager.ConnectionStrings["LinqChatConnectionString"].ConnectionString;
     using (SqlConnection con = new SqlConnection(strConnString))
     {
          using (SqlDataAdapter sda = new SqlDataAdapter())
          {
              cmd.Connection = con;
              sda.SelectCommand = cmd;
              using (DataSet ds = new DataSet())
              {
                  sda.Fill(ds, "Messages");
              }
          }
     }
}

那么如何在上述方法中获得Label / TextBox值?

我尝试过如下,但它的空值为

Page page = HttpContext.Current.Handler as Page;
Label lblRoomId = (Label)page.FindControl("lblRoomId");
string lbRoomId = lblRoomId.Text;

任何建议?

1 个答案:

答案 0 :(得分:0)

在参数中传递label / textbox值。

PageMethods.DeleteItem(txtValue);
public static void DeleteItem(string txtValue)
{
// perform operation
}