ASP .NET - TextBox 控件
TextBox 控件用于創(chuàng)建用戶可輸入文本的文本框。
TextBox 控件
TextBox 控件用于創(chuàng)建用戶可輸入文本的文本框。
TextBox 控件的屬性列在我們的 TextBox 控件參考手冊(cè)中。
下面的例子演示了您可能在 TextBox 控件中使用到的一些屬性:
<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è)置可通過(guò)服務(wù)器腳本進(jìn)行修改?赏ㄟ^(guò)點(diǎn)擊一個(gè)按鈕或當(dāng)用戶更改 TextBox 控件中的值對(duì)表單進(jìn)行提交。
在下面的例子中,我們?cè)谝粋(gè) .aspx 文件中聲明了一個(gè) TextBox 控件、一個(gè) Button 控件和一個(gè) Label 控件。當(dāng)提交按鈕被觸發(fā)時(shí),submit 子例程就會(huì)被執(zhí)行。submit 子例程會(huì)向 Label 控件寫一條文本:
<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>
在下面的例子中,我們?cè)谝粋(gè) .aspx 文件中聲明了一個(gè) TextBox 控件和一個(gè) Label 控件。當(dāng)您更改了 TextBox 中的值,并且在 TextBox 外單擊時(shí),change 子例程就會(huì)被執(zhí)行。change 子例程會(huì)向 Label 控件寫一條文本:
<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>