在代码背后调用输入类型

时间:2015-06-01 21:00:19

标签: c# html input

好的,我从不使用它:

<input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="Username" value="" />

我通常使用它:

<asp:TextBox type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="Username" runat="server"></asp:TextBox>

使用最后一个,我可以在后面的代码中调用文本框:

username.Text = "blah blah"

但是第一个我无法通过id来调用它。谁能告诉我如何在我的代码中调用第一个示例input type

2 个答案:

答案 0 :(得分:1)

<input type="text" name="username" id="username" t....

首先,您无法调用代码,因为它不是服务器端控件。您只能在代码中调用那些被定义为具有runat属性的服务器端控件的控件。

要在后面的代码中调用第一个,请添加runat="server"属性,如

<input type="text" name="username" runat="server" id="username" tabindex="1" class="form-control" placeholder="Username" value="" />

答案 1 :(得分:0)

runat属性添加到input,在后面的代码中,您可以使用输入ID在后面的代码中设置输入属性。

对于例子:你有这个文本框:

<input type="text" id="txt1" runat="server" />

并且您可以从后面的代码设置文本值:

txt1.Value = "my value";