TextBox Class

Displays a text box control for user input.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

C#
[ValidationPropertyAttribute("Text")] 
[ControlValuePropertyAttribute("Text")] 
public class TextBox : WebControl, IPostBackDataHandler, IEditableTextControl, ITextControl
J#
/** @attribute ValidationPropertyAttribute("Text") */ 
/** @attribute ControlValuePropertyAttribute("Text") */ 
public class TextBox extends WebControl implements IPostBackDataHandler, IEditableTextControl, 
	ITextControl
ValidationPropertyAttribute("Text") 
ControlValuePropertyAttribute("Text") 
public class TextBox extends WebControl implements IPostBackDataHandler, IEditableTextControl, 
	ITextControl

The TextBox server control is an input control that lets the user enter text. By default, the TextMode1 property of the control is set to TextBoxMode.SingleLine, which displays a single-line text box. However, you can also use the TextBox control to display a multiline text box or a text box that masks user input by changing the value of the TextMode property to TextBoxMode.MultiLine or TextBoxMode.Password, respectively. The text displayed in the TextBox control is specified or determined by using the Text2 property.

The TextBox control contains several properties that allow you to control the appearance of the control. The display width of the text box, in characters, is determined by its Columns3 property. If the TextBox control is a multiline text box, the number of rows it displays is determined by the Rows4 property. To display text that wraps within the TextBox control, set the Wrap5 property to true.

You can also specify how data is entered in the TextBox control by setting a few properties. To prevent the text displayed in the control from being modified, set the ReadOnly6 property to true. If you want to limit the user input to a specified number of characters, set the MaxLength7 property.

For a list of initial property values for an instance of TextBox, see the TextBox8 constructor.

Caution noteCaution

This control can be used to accept user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. You can use validation controls to verify user input before displaying the input text in a control. ASP.NET provides an input request validation feature to block script and HTML in user input. For more information, see Securing Standard Controls9How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings10, and Validating User Input in ASP.NET Web Pages11.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility12.

The following code example demonstrates how to use the TextBox control to get user input. When the user clicks the Add button, the sum of the values entered in the text boxes is displayed.

C#
<%@ Page Language="C#" AutoEventWireup="True" %>

<html> 

<head>

   <script runat="server">

      protected void AddButton_Click(Object sender, EventArgs e)
      {
         int Answer;

         Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text);

         AnswerMessage.Text = Answer.ToString();

      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> TextBox Example </h3>

      <table>

         <tr>

            <td colspan="5">

               Enter integer values into the text boxes. <br>
               Click the Add button to add the two values. <br>
               Click the Reset button to reset the text boxes.

            </td>

         </tr>

         <tr>

            <td colspan="5">

               &nbsp;

            </td>

         </tr>

         <tr align="center">

            <td>

               <asp:TextBox ID="Value1"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>

            </td>

            <td>

               + 

            </td>

            <td>

               <asp:TextBox ID="Value2"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>

            </td>

            <td>

               =

            </td>

            <td>
               
               <asp:Label ID="AnswerMessage"
                    runat="server"/>

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:RequiredFieldValidator
                    ID="Value1RequiredValidator"
                    ControlToValidate="Value1"
                    ErrorMessage="Please enter a value.<br>"
                    Display="Dynamic"
                    runat="server"/>

               <asp:RangeValidator
                    ID="Value1RangeValidator"
                    ControlToValidate="Value1"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br> between than 1 and 100.<br>"
                    Display="Dynamic"
                    runat="server"/>

            </td>

            <td colspan="2">

               <asp:RequiredFieldValidator
                    ID="Value2RequiredValidator"
                    ControlToValidate="Value2"
                    ErrorMessage="Please enter a value.<br>"
                    Display="Dynamic"
                    runat="server"/>

               <asp:RangeValidator
                    ID="Value2RangeValidator"
                    ControlToValidate="Value2"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br> between than 1 and 100.<br>"
                    Display="Dynamic"
                    runat="server"/>

            </td>

            <td>

               &nbsp
 
            </td

         </tr>

         <tr align="center">

            <td colspan="4">

               <asp:Button ID="AddButton"
                    Text="Add"
                    OnClick="AddButton_Click"
                    runat="server"/>

            </td>

            <td>

               &nbsp;

            </td>

         </tr>

      </table>

   </form>

</body>
</html>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements18.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Links Table
1http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textmode(v=vs.80).aspx
2http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.text(v=vs.80).aspx
3http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.columns(v=vs.80).aspx
4http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.rows(v=vs.80).aspx
5http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.wrap(v=vs.80).aspx
6http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly(v=vs.80).aspx
7http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.maxlength(v=vs.80).aspx
8http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textbox(v=vs.80).aspx
9http://msdn.microsoft.com/en-us/library/ms178270(v=vs.80).aspx
10http://msdn.microsoft.com/en-us/library/a2a4yykt(v=vs.80).aspx
11http://msdn.microsoft.com/en-us/library/7kh55542(v=vs.80).aspx
12http://msdn.microsoft.com/en-us/library/ms227996(v=vs.80).aspx
13http://msdn.microsoft.com/en-us/library/system.web.aspnethostingpermission(v=vs.80).aspx
14http://msdn.microsoft.com/en-us/library/system.security.permissions.securityaction(v=vs.80).aspx
15http://msdn.microsoft.com/en-us/library/system.object(v=vs.80).aspx
16http://msdn.microsoft.com/en-us/library/system.web.ui.control(v=vs.80).aspx
17http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol(v=vs.80).aspx
18http://msdn.microsoft.com/en-us/library/8z6watww(v=vs.80).aspx
19http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox_members(v=vs.80).aspx
20http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols(v=vs.80).aspx
21http://msdn.microsoft.com/en-us/library/91katfcc(v=vs.80).aspx
Community Content Add
Annotations FAQ