TextBox.Text TextBox.Text TextBox.Text TextBox.Text Property

定义

获取或设置 TextBox 控件的文本内容。Gets or sets the text content of the TextBox control.

public:
 virtual property System::String ^ Text { System::String ^ get(); void set(System::String ^ value); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.EncodedInnerDefaultProperty)]
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)]
public virtual string Text { get; set; }
member this.Text : string with get, set
Public Overridable Property Text As String

属性值

TextBox 控件中显示的文本。The text displayed in the TextBox control. 默认值为空字符串("")。The default is an empty string ("").

实现

示例

下面的代码示例演示如何使用TextBox控件获取用户输入。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.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。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="C#" AutoEventWireup="True" %>  
  
<!DOCTYPE html>  
<html>   
  
<head>  
    <title>TextBox Example</title>  
    <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 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>  
<%@ 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>  

注解

使用属性来指定或确定TextBox控件中显示的文本。 TextUse the Text property to specify or determine the text displayed in the TextBox control. 若要限制控件接受的字符数,请设置MaxLength属性。To limit the number of characters accepted by the control, set the MaxLength property. 如果要防止修改文本,请设置ReadOnly属性。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. 有关详细信息,请LocalizableAttribute参阅和ASP.NET 全球化和本地化For more information, see LocalizableAttribute and ASP.NET Globalization and Localization.

呈现文本框时,此属性的内容是 HTML 编码的。When the text box is rendered, the contents of this property are HTML-encoded. 例如,如果将此<属性设置为 "b > test</b >",则浏览器将显示 "<b > test</b >",而不是以粗体显示 "test"。For example, if you set this property to "<b>Test</b>", the browser will display "<b>Test</b>", not "Test" in bold.

适用于

另请参阅