top
Loading...
ASP.NET TextBox 控件

ASP.NET Web Forms - TextBox 控件


TextBox 控件用於創建用戶可輸入文本的文本框。


TextBox 控件

TextBox 控件用於創建用戶可輸入文本的文本框。

TextBox 控件的特性和屬性列在我們的 WebForms 控件參考手冊頁面

下面的實例演示了您可能會用到的 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>


添加腳本

當表單被提交時,TextBox 控件的內容和設置可能會被服務器腳本修改。表單可通過點擊一個按鈕或當用戶修改 TextBox 控件的值的時候進行提交。

在下面的實例中,我們在 .aspx 文件中聲明了一個 TextBox 控件、一個 Button 控件和一個 Label 控件。當提交按鈕被觸發時,submit 子例程將被執行。submit 子例程將寫入一行文本到 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>

在下面的實例中,我們在 .aspx 文件中聲明了一個 TextBox 控件和一個 Label 控件。當您修改了 TextBox 中的值,併且在 TextBox 外部點擊(或者按下了 Tab 鍵)時,change 子例程將會被執行。change 子例程將寫入一行文本到 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>


北斗有巢氏 有巢氏北斗