TextBox 类

为用户输入显示一个文本框控件。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<ValidationPropertyAttribute("Text")> _
<ControlValuePropertyAttribute("Text")> _
Public Class TextBox
    Inherits WebControl
    Implements IPostBackDataHandler, IEditableTextControl, ITextControl
用法
Dim instance As TextBox
[ValidationPropertyAttribute("Text")] 
[ControlValuePropertyAttribute("Text")] 
public class TextBox : WebControl, IPostBackDataHandler, IEditableTextControl, ITextControl
[ValidationPropertyAttribute(L"Text")] 
[ControlValuePropertyAttribute(L"Text")] 
public ref class TextBox : public 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

备注

TextBox 服务器控件是使用户可以输入文本的输入控件。默认情况下,该控件的 TextMode 属性设置为 TextBoxMode.SingleLine,将显示一个单行文本框。然而,通过将 TextMode 属性值分别改为 TextBoxMode.MultiLineTextBoxMode.Password,您也可以使用 TextBox 控件显示多行文本框或显示屏蔽用户输入的文本框。使用 Text 属性,可以指定或确定 TextBox 控件中显示的文本。

TextBox 控件包含多个属性,用于控制该控件的外观。文本框的显示宽度(以字符为单位)由它的 Columns 属性确定。如果 TextBox 控件是多行文本框,则它显示的行数由 Rows 属性确定。要在 TextBox 控件中显示换行文本,请将 Wrap 属性设置为 true

还可以设置一些属性来指定如何将数据输入到 TextBox 控件中。要防止控件中显示的文本被修改,请将 ReadOnly 属性设置为 true。如果想限定用户只能输入指定数目的字符,请设置 MaxLength 属性。

有关 TextBox 的实例的初始属性值列表,请参见 TextBox 构造函数。

警告

此控件可用来接受用户输入,而输入可能包含恶意的客户端脚本。在您的应用程序中显示从客户端发送来的信息之前,先检查这些信息中是否有可执行脚本、SQL 语句或其他代码。可以在将输入文本显示在控件中之前使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,可以阻止用户输入中的脚本和 HTML 代码。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

辅助功能

默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:到自定义业务对象的数据绑定 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中使用级联样式表样式 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:验证 Web 窗体页中的用户输入 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建基本网页 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 使用 Visual Web Developer 生成应用程序
演练:验证 Web 窗体页中的用户输入 使用 Visual Web Developer 生成应用程序
如何:获取和设置 TextBox Web 服务器控件中的值 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 TextBox Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 ASP.NET Web 服务器控件属性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 TextBox Web 服务器控件以输入密码 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建基本网页 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:为 TextBox Web 服务器控件指定多行输入 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:获取和设置 TextBox Web 服务器控件中的值 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 TextBox Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 ASP.NET Web 服务器控件属性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 TextBox Web 服务器控件以输入密码 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建基本网页 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:为 TextBox Web 服务器控件指定多行输入 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:获取和设置 TextBox Web 服务器控件中的值 生成 ASP .NET Web 应用程序
如何:响应 TextBox Web 服务器控件中的更改 生成 ASP .NET Web 应用程序
如何:设置 ASP.NET Web 服务器控件属性 生成 ASP .NET Web 应用程序
如何:设置 TextBox Web 服务器控件以输入密码 生成 ASP .NET Web 应用程序
如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入 生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 生成 ASP .NET Web 应用程序
如何:为 TextBox Web 服务器控件指定多行输入 生成 ASP .NET Web 应用程序

示例

下面的代码示例演示如何使用 TextBox 控件来获取用户输入。当用户单击“添加”按钮时,将显示文本框中输入值之和。

<%@ Page Language="VB" AutoEventWireup="True" %>

<html> 

<head>

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

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.TextBox

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

TextBox 成员
System.Web.UI.WebControls 命名空间
TextMode
Text
Columns
Rows
Wrap
ReadOnly
MaxLength

其他资源

TextBox Web 服务器控件