c#如何访问 Asp 控制内的静态方法?

标签: Asp.net C#
发布时间: 2017/2/25 3:49:50
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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 页中我对我的要求涉及两个文本框。但在我的后端,无法访问在静态方法中的文本框。提出一些想法。赞赏是答案,而不是评论。

解决方法 1:

试试这个 !

 [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;
                   }
              }
           }
        }

是指

官方微信
官方QQ群
31647020