gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:00:54 27 4
gpt4 key购买 nike

我想从 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;

有什么建议吗?

最佳答案

在参数中传递标签/文本框值。

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

关于javascript - C# 如何在静态Web方法中获取asp.net文本框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32153264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com