Language

PagesSection.AutoEventWireup プロパティ

定義

ASP.NET ページのイベントがイベント処理関数に自動的に接続されるかどうかを示す値を取得または設定します。

public:
 property bool AutoEventWireup { bool get(); void set(bool value); };
[System.Configuration.ConfigurationProperty("autoEventWireup", DefaultValue=true)]
public bool AutoEventWireup { get; set; }
[<System.Configuration.ConfigurationProperty("autoEventWireup", DefaultValue=true)>]
member this.AutoEventWireup : bool with get, set
Public Property AutoEventWireup As Boolean

プロパティ値

true ASP.NET ページのイベントがイベント処理関数に自動的に接続される場合。それ以外の場合は false。 既定値は true です。

属性

例

次のコード例は、コードで AutoEventWireup プロパティを設定または読み取る方法を示しています。

// Get the current AutoEventWireup property value.
Console.WriteLine(
    "Current AutoEventWireup value: '{0}'",
    pagesSection.AutoEventWireup);

// Set the AutoEventWireup property to false.
pagesSection.AutoEventWireup = false;
' Get the current AutoEventWireup property value.
Console.WriteLine( _
    "Current AutoEventWireup value: '{0}'", _
    pagesSection.AutoEventWireup)

' Set the AutoEventWireup property to false.
pagesSection.AutoEventWireup = False

次の例は、 AutoEventWireup が trueされたときにページ イベントに自動的にアタッチされる 2 つの形式のメソッド シグネチャを示しています。

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
// This method will be automatically bound to the Load event
// when AutoEventWireup is true.
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello world");
}
// This method will be automatically bound to the Load event 
// when AutoEventWireup is true only if no overload having 
// object and EventArgs parameters is found.
protected void Page_Load()
{
    Response.Write("Hello world");
}
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
' This method will be automatically bound to the Load event
' when AutoEventWireup is true.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("Hello world")
End Sub
' This method will be automatically bound to the Load event 
' when AutoEventWireup is true only if no overload having 
' object and EventArgs parameters is found.    
Protected Sub Page_Load()
    Response.Write("Hello world")
End Sub

次の例は、 AutoEventWireup が falseされたときにイベントを明示的に結び付ける方法を示しています。

// Following are three alternative ways of binding an event
// handler to an event when AutoEventWireup is false.  For
// any given event do this binding only once or the handler
// will be called multiple times.

// You can wire up events in the page's constructor.
public _Default()
{
    Load += new EventHandler(Page_Load);
}

// You can override the OnInit event and wire up events there.
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Load += new EventHandler(Page_Load);
}

// Or you can override the event's OnEventname method and
// call your handler from there.  You can also put the code
// execute when the event fires within the override method itself.
protected override void OnLoad(EventArgs e)
{
    Page_Load(null, null);
    base.OnLoad(e);
}

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello world");
}
' The Handles keyword binds Page_Load to the Load event.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Write("Hello world")
End Sub

注釈

AutoEventWireupがtrueされている場合、ASP.NET では、Loadなどのページ イベントにイベント ハンドラーを明示的にバインドする必要はありません。

AutoEventWireupがfalseされている場合は、イベントをメソッドに明示的にバインドする必要があります。 たとえば、ページのコードに Page_Load メソッドがある場合、次の例のようなコードを記述した場合にのみ、 Load イベントに応答してメソッドが呼び出されます (Visual Basic の Handles ステートメントと C# のイベント ハンドラー コードに注意してください)。

Partial Class AutoEventWireupExample
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Load
        Response.Write("Executing Page_Load")
    End Sub
End Class
public partial class AutoEventWireupExample : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        Response.Write("Executing Page_Load");
    }
    override protected void OnInit(EventArgs e)
    {
        this.Load += new System.EventHandler(this.Page_Load);
    }
}

AutoEventWireupがtrueされると、ハンドラーは、その名前と署名に基づいて、実行時にイベントに自動的にバインドされます。 イベントごとに、ASP.NET は、Page_やなどのPage_Load に従って名前が付けられたメソッドを検索します。 ASP.NET 最初に、一般的なイベント ハンドラーシグネチャを持つオーバーロードをチェックします (つまり、 Object パラメーターと EventArgs パラメーターを指定します)。 このシグネチャを持つイベント ハンドラーが見つからない場合は、パラメーターのないオーバーロード ASP.NET チェックします。

AutoEventWireupがfalseされている場合は、前の例に示すように、イベント ハンドラーをイベントに明示的にバインドする必要があります。 その場合、メソッド名はパターンに従う必要はありません。

true ディレクティブでAutoEventWireupが指定されていない場合、既定値は@ Pageされます。 Visual Studio では、分離コード ファイルの作成時に属性が自動的に含まれます。 C# で記述された ASP.NET ページの場合、Visual Studio は値を true に設定します。 Visual Basic の場合、ハンドラーは Handles キーワードを使用してイベントにバインドされるため、Visual Studio は値を false に設定します。 これは 、イベント ハンドラーの生成時に Visual Studio によって自動的に挿入されるためです。 AutoEventWireupを true に設定した場合は、Handles キーワードを省略 (または削除) できます。

パフォーマンスが重要な考慮事項である場合は、 AutoEventWireup を true に設定しないでください。 イベントの自動ワイヤアップが有効になっている場合、ASP.NET は 15 から 30 の間でイベントとメソッドの照合を試行する必要があります。

イベント ハンドラーをイベントにバインドする方法については、次の点に注意してください。

  • AutoEventWireupを true に設定した場合は、ページ イベント ハンドラーをイベントに手動でアタッチしないようにしてください。 その場合、ハンドラーが複数回呼び出される可能性があります。

  • 自動バインドはページ イベントに対してのみ実行され、ページ上のコントロールのイベントに対しては実行されません。

  • イベントをハンドラーにバインドする代わりに、ページまたはコントロールの Oneventname メソッドをオーバーライドできます。

適用対象