当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET入门教程:TextBox控件

ASP.NET
Web服务器控件:LinkButton控件
Web服务器控件:ListBox控件
Web服务器控件:ListItem控件
Web服务器控件:Literal控件
Web服务器控件:Panel控件
Web服务器控件:PlaceHolder控件
Web服务器控件:RadioButton控件
Web服务器控件:RadioButtonList控件
Web服务器控件:BulletedList控件
Web服务器控件:Style控件
Web服务器控件:Table控件
Web服务器控件:TableCell控件
Web服务器控件:TableRow控件
Web服务器控件:TextBox控件
Web服务器控件:XML控件
Validation服务器控件:CompareValidator控件
Validation服务器控件:CustomValidator控件
Validation服务器控件:RangeValidator控件
Validation服务器控件:RegularExpressionValidator控件
Validation服务器控件:RequiredFieldValidator控件

ASP.NET入门教程:TextBox控件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 52 ::
收藏到网摘: n/a

控件用于创建用户可输入文本的文本框。

TextBox 控件

TextBox 控件用于创建用户可输入文本的文本框。

TextBox 控件的属性列在我们的TextBox 控件参考手册中。

属性

属性 描述 .NET
AutoCompleteType 规定 TextBox 控件的 AutoComplete 行为。 2.0
AutoPostBack 布尔值,规定当内容改变时,是否回传到服务器。默认是 false。 1.0
CausesValidation 规定当 Postback 发生时,是否验证页面。 2.0
Columns textbox 的宽度。 1.0
MaxLength 在 textbox 中所允许的最大字符数。 1.0
ReadOnly 规定能否改变文本框中的文本。 1.0
Rows textbox 的高度(仅在 TextMode="Multiline" 时使用)。 1.0
runat 规定该控件是否是服务器控件。必须设置为 "server"。  
TagKey    
Text textbox 的内容。 1.0
TextMode 规定 TextBox 的行为模式(单行、多行或密码)。 1.0
ValidationGroup 当 Postback 发生时,被验证的控件组。  
Wrap 布尔值,指示 textbox 的内容是否换行。 1.0
OnTextChanged 当 textbox 中的文本被更改时,被执行的函数的名称。  

下面的例子演示了您可能在 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>

添加脚本

在下面的例子中,我们在一个 .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 外单击时,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>