The TextBox Control
The TextBox control is used to create a text box where the user can input text.
The example below demonstrates some of the attributes you may use with the TextBox control:
<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>
|
Add a Script
The contents and settings of a TextBox control may be changed by server scripts when a form is submitted. A form can be submitted by clicking on a button or when a user changes the value in the TextBox control.
In the following example we declare one TextBox control, one Button control, and one Label control in an .aspx file. When the submit button is triggered, the submit subroutine is executed. The submit subroutine writes a text to the Label control:
<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>
|
In the following example we declare one TextBox control and one Label control in an .aspx file. When you change the value in the TextBox and either click outside the TextBox or press the Tab key, the change subroutine is executed. The submit subroutine writes a text to the Label control:
<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>
|
Properties
Property |
Description |
AutoPostBack |
A Boolean value that specifies whether the control is automatically posted back to the server when the contents change or not. Default is false |
Columns |
The width of the textbox |
id |
A unique id for the control |
MaxLength |
The maximum number of characters allowed in the textbox |
OnTextChanged |
The name of the function to be executed when the text in the textbox has changed |
Rows |
The height of the textbox (only used if TextMode="Multiline") |
runat |
Specifies that the control is a server control. Must be set to "server" |
Text |
The contents of the textbox |
TextMode |
SingleLine creates a text box with only one line. MultiLine creates a text box with multiple lines. Password creates a one-line text box that masks the value entered by the user. Default is SingleLine. Legal values are:
- SingleLine
- MultiLine
- Password
|
Wrap |
A Boolean value that indicates whether the contents of the textbox should wrap or not |
|