TextBox Class
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 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 |
|---|
| 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 Controls9, How 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.
<%@ 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"> </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>   </td </tr> <tr align="center"> <td colspan="4"> <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/> </td> <td> </td> </tr> </table> </form> </body> </html>
- AspNetHostingPermission13 for operating in a hosted environment. Demand value: LinkDemand14; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand14; Permission value: Minimal.
System.Web.UI.Control16
System.Web.UI.WebControls.WebControl17
System.Web.UI.WebControls.TextBox
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.
Reference
TextBox Members19System.Web.UI.WebControls Namespace20
TextMode1
Text2
Columns3
Rows4
Wrap5
ReadOnly6
MaxLength7

Caution