gpt4 book ai didi

c# - 如何在静态方法中访问 Asp 控件?

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:46 25 4
gpt4 key购买 nike

ASP:代码

<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly="true"></asp:TextBox>

C#:

[System.Web.Services.WebMethod]
public static Array LoadAssetAssignView() {
string sql = "SELECT Time,Inuse FROM table4";
using(SqlConnection Connection = new SqlConnection((@ "Data Source"))) {
using(SqlCommand myCommand = new SqlCommand(sql, Connection)) {
Connection.Open();
using(SqlDataReader myReader = myCommand.ExecuteReader()) {
DataTable dt = new DataTable();
dt.Load(myReader);
Connection.Close();
Num1 = textbox1.text; //Error(Can't access my asp control)
Num2 = textbox2.text;
}
}
}
}

在我的 Asp 页面中,我涉及两个文本框以满足我的要求。但在我的后端,我无法访问静态方法中的文本框。建议一些想法。感谢回答而不是评论。

最佳答案

试试这个!

 [System.Web.Services.WebMethod]
public static Array LoadAssetAssignView()
{
string sql = "SELECT Time,Inuse FROM table4";
using (SqlConnection Connection = new SqlConnection((@"Data Source")))
{
using (SqlCommand myCommand = new SqlCommand(sql, Connection))
{
Connection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(myReader);
Connection.Close();
Page page = (Page)HttpContext.Current.Handler;
TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");
TextBox TextBox2 = (TextBox)page.FindControl("TextBox2");
Num1=TextBox1 .text;
Num2=TextBox2 .text;
}
}
}
}

引用this .

关于c# - 如何在静态方法中访问 Asp 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41374996/

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