Share via


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

プロパティ値

ASP.NET ページのイベントを自動的にイベント処理関数に渡す場合は true。それ以外の場合は 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

次の例は、 が の場合AutoEventWireuptrueにページ イベントに自動的にアタッチされる 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

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

// 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

注釈

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

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

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);
    }
}

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

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

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

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

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

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

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

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

適用対象