TextBox.Text Property
Gets or sets the text content of the TextBox control.
Assembly: System.Web (in System.Web.dll)
<BindableAttribute(True, BindingDirection.TwoWay)> <PersistenceModeAttribute(PersistenceMode.EncodedInnerDefaultProperty)> Public Overridable Property Text As String
Property Value
Type: System.StringThe text displayed in the TextBox control. The default is an empty string ("").
Implements
ITextControl.TextUse the Text property to specify or determine the text displayed in the TextBox control. To limit the number of characters accepted by the control, set the MaxLength property. If you want to prevent the text from being modified, set the ReadOnly property.
The value of this property, when set, can be saved automatically to a resource file by using a designer tool. For more information, see LocalizableAttribute and ASP.NET Globalization and Localization.
When the text box is rendered, the contents of this property are HTML-encoded. For example, if you set this property to "<b>Test</b>", the browser will display "<b>Test</b>", not "Test" in bold.
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.
Security Note
|
|---|
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview. |
<%@ Page Language="vb" AutoEventWireup="false" %> <!DOCTYPE html> <html> <head> <title>TextBox Example</title> <script runat="server"> Protected Sub AddButton_Click(sender As Object, e As EventArgs) Dim Answer As Integer Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text) AnswerMessage.Text = Answer.ToString() End Sub </script> </head> <body> <form id="form1" runat="server"> <h3> TextBox Example </h3> <p> <asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/> + <asp:TextBox ID="Value2" Columns="2" MaxLength="3" Text="1" runat="server"/> = <asp:Label ID="AnswerMessage" runat="server"/> </p> <p> <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/> </p> <p> <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1" ErrorMessage="Please enter a value.<br />" Display="Dynamic" runat="server"/> <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2" ErrorMessage="Please enter a value.<br />" Display="Dynamic" runat="server"/> <asp:RangeValidator ID="Value1RangeValidator" ControlToValidate="Value1" Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic" ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />" runat="server"/> <asp:RangeValidator ID="Value2RangeValidator" ControlToValidate="Value2" Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic" ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />" runat="server"/> </p> </form> </body> </html>
Available since 1.1
