TextBox.TextChanged 事件
定义
当文本框的内容在向服务器的各次发送过程间更改时发生。Occurs when the content of the text box changes between posts to the server.
public:
event EventHandler ^ TextChanged;
public:
virtual event EventHandler ^ TextChanged;
public event EventHandler TextChanged;
member this.TextChanged : EventHandler
Public Custom Event TextChanged As EventHandler
事件类型
实现
示例
下面的示例演示如何使用此事件来响应控件中的更改 TextBox 。The following example shows how you can use this event to respond to changes in the TextBox control. 此代码将 Text 在属性更改时,显示名为) Label1 的标签 ( (名为 TextBox1) 的 TextBox 控件的属性内容 Text 。The code displays the contents of the Text property of a TextBox control (called TextBox1) in a label (called Label1) when the Text property is changed. Web 窗体页中的用户输入可能包含潜在的恶意客户端脚本。User input in a Web Forms page can include potentially malicious client script. 默认情况下,Web 窗体页验证用户输入不包含脚本或 HTML 元素。By default, the Web Forms page validates that user input does not include script or HTML elements. 有关详细信息,请参阅 如何:通过将 HTML 编码应用于字符串来防范 Web 应用程序中的脚本攻击。For more information, see How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings.
Protected Sub TextBox1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Label1.Text = Server.HtmlEncode(TextBox1.Text)
End Sub
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = Server.HtmlEncode(TextBox1.Text);
}
注解
TextChanged当文本框的内容在向服务器的各次发送过程中发生更改时,将引发事件。The TextChanged event is raised when the content of the text box changes between posts to the server. 仅当用户更改文本时才引发事件;如果以编程方式更改文本,则不会引发事件。The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.
备注
TextBox控件必须在向服务器的各次发送过程中保存一些值才能使此事件正常工作。A TextBox control must persist some values between posts to the server for this event to work correctly. 确保为此控件启用了视图状态。Be sure that view state is enabled for this control.
有关如何处理事件的详细信息,请参阅 处理和引发事件。For more information about how to handle events, see Handling and Raising Events.