TextBox.OnTextChanged(EventArgs) 方法

定义

引发 TextChanged 事件。 这使您可以直接处理该事件。

protected:
 virtual void OnTextChanged(EventArgs ^ e);
protected virtual void OnTextChanged (EventArgs e);
abstract member OnTextChanged : EventArgs -> unit
override this.OnTextChanged : EventArgs -> unit
Protected Overridable Sub OnTextChanged (e As EventArgs)

参数

e
EventArgs

EventArgs,它包含事件信息。

示例

下面的代码示例演示如何重写 方法, OnTextChanged 使其始终将自定义 TextBox 服务器控件标记为已修改。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Custom TextBox - OnTextChanged - C# Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom TextBox - OnTextChanged - C# Example</h3>
            
            <aspSample:CustomTextBoxOnTextChanged 
              id="TextBox1" 
              autopostback=true
              runat="server">Hello World!
            </aspSample:CustomTextBoxOnTextChanged>
            
        </form>
    </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Custom TextBox - OnTextChanged - VB.NET Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom TextBox - OnTextChanged - VB.NET Example</h3>
            
            <aspSample:CustomTextBoxOnTextChanged id="TextBox1" autopostback=true
             runat="server">Hello World!</aspSample:CustomTextBoxOnTextChanged>
        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomTextBoxOnTextChanged : System.Web.UI.WebControls.TextBox
  {
    private bool isDirty = false;

    protected override void OnTextChanged(System.EventArgs e)
    {
      // Call the base OnTextChanged method.
      base.OnTextChanged(e);

      // Change the dirty flag to True.
      isDirty = true;
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomTextBoxOnTextChanged
        Inherits System.Web.UI.WebControls.TextBox

        Private isDirty As Boolean = False

        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)

            ' Call the base OnTextChanged method.
            MyBase.OnTextChanged(e)

            ' Change the dirty flag to True.
            isDirty = True
        End Sub
    End Class
End Namespace

注解

TextChanged 文本框的内容在发到服务器的帖子之间发生更改时,将引发 该事件。

注意

控件 TextBox 必须在发帖到服务器之间保留一些值,才能使此事件正常工作。 确保为此控件启用了视图状态。

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnTextChanged 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnTextChanged(EventArgs) 时,一定要调用基类的 OnTextChanged(EventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅