ASP.NET TextBox 控件
asp.net web forms - textbox 控件
textbox 控件用于創(chuàng)建用戶可輸入文本的文本框。
textbox 控件
textbox 控件用于創(chuàng)建用戶可輸入文本的文本框。
textbox 控件的特性和屬性列在我們的 webforms 控件參考手冊(cè)頁(yè)面。
下面的實(shí)例演示了您可能會(huì)用到的 textbox 控件的一些屬性:
實(shí)例
<html>
<body>
<form runat="server">
a basic textbox:
<asp:textbox id="tb1" runat="server" />
<br /><br />
a password textbox:
<asp:textbox id="tb2" textmode="password" runat="server" />
<br /><br />
a textbox with text:
<asp:textbox id="tb4" text="hello world!" runat="server" />
<br /><br />
a multiline textbox:
<asp:textbox id="tb3" textmode="multiline" runat="server" />
<br /><br />
a textbox with height:
<asp:textbox id="tb6" rows="5" textmode="multiline"
runat="server" />
<br /><br />
a textbox with width:
<asp:textbox id="tb5" columns="30" runat="server" />
</form>
</body>
</html>
<body>
<form runat="server">
a basic textbox:
<asp:textbox id="tb1" runat="server" />
<br /><br />
a password textbox:
<asp:textbox id="tb2" textmode="password" runat="server" />
<br /><br />
a textbox with text:
<asp:textbox id="tb4" text="hello world!" runat="server" />
<br /><br />
a multiline textbox:
<asp:textbox id="tb3" textmode="multiline" runat="server" />
<br /><br />
a textbox with height:
<asp:textbox id="tb6" rows="5" textmode="multiline"
runat="server" />
<br /><br />
a textbox with width:
<asp:textbox id="tb5" columns="30" runat="server" />
</form>
</body>
</html>
添加腳本
當(dāng)表單被提交時(shí),textbox 控件的內(nèi)容和設(shè)置可能會(huì)被服務(wù)器腳本修改。表單可通過點(diǎn)擊一個(gè)按鈕或當(dāng)用戶修改 textbox 控件的值的時(shí)候進(jìn)行提交。
在下面的實(shí)例中,我們?cè)?.aspx 文件中聲明了一個(gè) textbox 控件、一個(gè) button 控件和一個(gè) label 控件。當(dāng)提交按鈕被觸發(fā)時(shí),submit 子例程將被執(zhí)行。submit 子例程將寫入一行文本到 label 控件中:
實(shí)例
<script runat="server">
sub submit(sender as object, e as eventargs)
lbl1.text="your name is " & txt1.text
end sub
</script>
<html>
<body>
<form runat="server">
enter your name:
<asp:textbox id="txt1" runat="server" />
<asp:button onclick="submit" text="submit" runat="server" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>
sub submit(sender as object, e as eventargs)
lbl1.text="your name is " & txt1.text
end sub
</script>
<html>
<body>
<form runat="server">
enter your name:
<asp:textbox id="txt1" runat="server" />
<asp:button onclick="submit" text="submit" runat="server" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>
在下面的實(shí)例中,我們?cè)?.aspx 文件中聲明了一個(gè) textbox 控件和一個(gè) label 控件。當(dāng)您修改了 textbox 中的值,并且在 textbox 外部點(diǎn)擊(或者按下了 tab 鍵)時(shí),change 子例程將會(huì)被執(zhí)行。change 子例程將寫入一行文本到 label 控件中:
實(shí)例
<script runat="server">
sub change(sender as object, e as eventargs)
lbl1.text="you changed text to " & txt1.text
end sub
</script>
<html>
<body>
<form runat="server">
enter your name:
<asp:textbox id="txt1" runat="server"
text="hello world!"
ontextchanged="change" autopostback="true"/>
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>
sub change(sender as object, e as eventargs)
lbl1.text="you changed text to " & txt1.text
end sub
</script>
<html>
<body>
<form runat="server">
enter your name:
<asp:textbox id="txt1" runat="server"
text="hello world!"
ontextchanged="change" autopostback="true"/>
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>