我想从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;
任何建议?
答案 0 :(得分:0)
在参数中传递label / textbox值。
PageMethods.DeleteItem(txtValue);
public static void DeleteItem(string txtValue)
{
// perform operation
}