-4

//aspx文件

<input id="Text1" type="text" runat="server"/><br/>
<input id="Button1" type="button" value="button" onclick="Button1_Click" />

// cs文件

protected void Button1_Click(object sender, EventArgs e)
{
  //There are so many control.I am accessing value in string
  string s1 = "Text1";
  TextBox AgeTextBox = Page.FindControl(s1) as TextBox;

  AgeTextBox.Text;           
 }
4

2 回答 2

1

假设您使用的是文字 HTML 输入,由ASP.NETHtmlInputText中的 an 表示:

1)添加runat属性表示该控件将在服务器上运行,然后您可以直接从后面的代码中访问它:

<input id="Text1" type="text" runat="server"/><br/>

2)从名称访问它:

protected void Button1_Click(object sender, EventArgs e)
{
  /*Dos stuff*/
  string text = Text1.Value;
}

有关HTML 输入控件的更多信息。

于 2013-09-23T18:37:44.477 回答
0

您还可以使用 name 属性访问 html 文本框的值。(如果你不想让它成为服务器控件)

 <input id="Text1" type="text" name="txtname"/><br/>
    <input id="Button1" type="button" value="button" onclick="Button1_Click" />

// cs文件

protected void Button1_Click(object sender, EventArgs e)
        {

            string ttext = Request["txtname"];

        }
于 2013-09-23T18:42:12.587 回答