ASP.Net TextBox Control

Updated on 25 Jul 2012,
Published on 09 Apr 2010

The ASP.Net TextBox control enables you to display an input type textbox control on the web page that allows the users to enter their input in text mode and submit it to the server side using ASP.Net Button control. You can get or set the value to or from the ASP.Net Textbox control using its Text property programmatically. It support both plain text as well as HTML code or scripts that can be submitted to the server side or can be displayed in the multiline enabled textbox control. The HTML code and CSS support of textbox control enables you to customize the styles of the text entered through it. This feature also leads to security vulnerabilities. That is why it is recommended to validate the input entered by the user into the ASP.Net textbox control to prevent the execution of malicious client side scripts. You can also attach the client side events to the textbox control that can be used to validate the user input.

TextBox Control Examples:

You can see the live samples and examples of TextBox Control from the following links:

Properties of ASP.Net TextBox Control

The TextBox control consists of two types of properties such as CSS based style properties that allow you to customize the look and feel of the control as well as the input text entered by the users and the other type of properties handles the behavior of textbox.

Following are the commonly used properties of ASP.Net Textbox control:

1. AutoCompleteType: It accepts the predefined keyword for example FirstName, LastName, Email etc. that provides the functionality to display the autocomplete list under the textbox control to enter the input from the previously entered values for the textboxes with same AutoCompleteType.

2. AutoPostBack: It is a Boolean type property that enables or disables the auto postback functionality of text box control that posts the web page to server when user enters the input into the textbox control jumps to the another control.

3. CausesValidation: It is also a Boolean type property that enables or disables the validation controls associated with the textbox control to validate the input.

4. Columns: It accepts the integer value to set the width of the multiline textbox. The integer value enables the control to accommodate the specified number of characters horizontally.

5. MaxLength: It also accepts the integer value that restricts the control to reject the characters exceeding the specified value. This property works with single line textbox control

6. ReadOnly: It is a Boolean property that enables or disables the textbox control whether to allow the user to change its value or not.

7. Rows: It accepts the integer value to set the height of the multiline textbox. The integer value enables the control to accommodate the specified number of characters vertically.

8. Text: It accepts the text value that you want to display in the textbox control initially. You can assign the value dynamically as well as get the value entered by the user through the Text property of ASP.Net textbox control.

9. TextMode: It accepts the predefined keyword such as SingleLine, MultiLine or Password that adjusts the behavior of the textbox control on the web page.

10. ValidationGroup: It accepts the name of the validation group to which it belongs that validates the user input when a postback occurs.

Other than the above properties it also contains CSS based properties that allow you to customize the style of the textbox and the input text. These properties include BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Font-Bold, Font-Italic, Font-Names, Font-Overline, Font-Size, Font-StrikeOut, Font-Underline, ForeColor, Height and Width.

ASP.Net TextBox Events

The textbox control provides OnTextChanged event that can be used to read and validate the user input entered into the textbox.

Code For ASP.Net TextBox Control Text Property

<table width="400" border="0" cellpadding="5" cellspacing="0" align="center">
    <tr width="100">
        <td><asp:Label ID="Label1" runat="server" Font-Size="13px" Text="Enter text: "></asp:Label></td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Submit" />
            <asp:Label ID="Label2" runat="server" Font-Bold="true" Font-Size="13px" Text="Output: "></asp:Label>
        <td colspan="2">
            <asp:Label ID="Label3" runat="server" Font-Size="13px"></asp:Label>
C# Code
protected void Page_Load(object sender, EventArgs e)
    Button1.Click += new EventHandler(Button1_Click);    

void Button1_Click(object sender, EventArgs e)
    Label3.Text = string.Format("You entered: <b>{0}</b>", TextBox1.Text);


Get the output for above discussed code from the following link:

TextBox Control Text Property

Continue to the next tutorial: ASP.Net TextBox TextMode Property to learn how to change the behavior of textbox to accept the single line, multiline or password type text inputs by the user.

1 Responses to "ASP.Net TextBox Control"
Sorry ,I still don't understand !In other words,what's the real function of it!
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email