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 サーバー コントロールが常に変更済みとしてマークされるようにする方法を示します。

Important

この例には、潜在的なセキュリティ上の脅威であるユーザー入力を受け入れるテキスト ボックスがあります。 既定では、ASP.NET Web ページでは、ユーザー入力にスクリプトや 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 イベントは、サーバーへの投稿間でテキスト ボックスの内容が変更されたときに発生します。

Note

TextBox コントロールは、このイベントが正常に動作するために、サーバーへの投稿間にいくつかの値を保持する必要があります。 このコントロールに対してビューステートが有効になっていることを確認します。

イベントを発生させると、デリゲートを介してイベント ハンドラーが呼び出されます。 詳細については、「イベントの 処理と発生」を参照してください。

OnTextChanged メソッドでは、デリゲートをアタッチせずに、派生クラスでイベントを処理することもできます。 これは、派生クラスでイベントを処理するために推奨される手法です。

注意 (継承者)

派生クラスで OnTextChanged(EventArgs) をオーバーライドする場合は、登録されているデリゲートがイベントを受け取るように、基底クラスの OnTextChanged(EventArgs) メソッドを必ず呼び出してください。

適用対象

こちらもご覧ください