<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.MultiLine 或 TextBoxMode.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 控件和辅助功能。
下面的代码示例演示如何使用 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"> </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>
<%@ 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>
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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。