Export (0) Print
Expand All
8 out of 15 rated this helpful - Rate this topic

TextBox Class

Displays a text box control for user input.

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

[ValidationPropertyAttribute("Text")] 
[ControlValuePropertyAttribute("Text")] 
public class TextBox : WebControl, IPostBackDataHandler, IEditableTextControl, ITextControl
/** @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 TextMode 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 Text 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 Columns property. If the TextBox control is a multiline text box, the number of rows it displays is determined by the Rows property. To display text that wraps within the TextBox control, set the Wrap 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 ReadOnly property to true. If you want to limit the user input to a specified number of characters, set the MaxLength property.

For a list of initial property values for an instance of TextBox, see the TextBox 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 ControlsHow to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Validating User Input in ASP.NET Web Pages.

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 Accessibility.

TopicLocation
How to: Get and Set Values in TextBox Web Server ControlsBuilding ASP .NET Web Applications
How to: Respond to Changes in a TextBox Web Server ControlBuilding ASP .NET Web Applications
How to: Set ASP.NET Web Server Control PropertiesBuilding ASP .NET Web Applications
How to: Set a TextBox Web Server Control for Password EntryBuilding ASP .NET Web Applications
How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to StringsBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Specify Multiline Input for a TextBox Web Server ControlBuilding ASP .NET Web Applications
How to: Get and Set Values in TextBox Web Server ControlsBuilding ASP .NET Web Applications
How to: Respond to Changes in a TextBox Web Server ControlBuilding ASP .NET Web Applications
How to: Set ASP.NET Web Server Control PropertiesBuilding ASP .NET Web Applications
How to: Set a TextBox Web Server Control for Password EntryBuilding ASP .NET Web Applications
How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to StringsBuilding ASP .NET Web Applications
How to: Set Focus on ASP.NET Web Server ControlsBuilding ASP .NET Web Applications
How to: Specify Multiline Input for a TextBox Web Server ControlBuilding ASP .NET Web Applications
Walkthrough: Working with Cascading Style Sheet Styles in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Validating User Input in a Web Forms PageBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Data Binding to a Custom Business ObjectBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Basic Web Page in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio

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.

<%@ 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>

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.TextBox
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 Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.