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

下列範例顯示當 為 trueAutoEventWireup 自動附加至頁面事件的兩種方法簽章。

<%@ 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

下列範例示範如何在 是 falseAutoEventWireup 明確連接事件。

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

備註

當 為 trueAutoEventWireup ,ASP.NET 不需要您明確地將事件處理常式系結至頁面事件,例如 Load

當 為 falseAutoEventWireup ,您必須明確地將 事件系結至 方法。 例如,如果您在 Page_Load 頁面的程式碼中有方法,只有在您在下列範例中撰寫類似的程式碼時,才會呼叫 方法以回應 Load 事件, (請注意 Handles Visual Basic 中的 語句和 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);
    }
}

當 為 trueAutoEventWireup ,處理常式會在執行時間根據事件名稱和簽章自動系結至事件。 針對每個事件,ASP.NET 搜尋根據 pattern Page_eventname命名的方法,例如 Page_LoadPage_Init 。 ASP.NET 會先檢查具有一般事件處理常式簽章 (的多載,它會指定 ObjectEventArgs 參數) 。 如果找不到具有此簽章的事件處理常式,ASP.NET 會檢查沒有參數的多載。

當 為 falseAutoEventWireup ,您必須明確地將事件處理常式系結至事件,如上述範例所示。 在此情況下,方法名稱不需要遵循模式。

如果 AutoEventWireup 未在 指示詞中 @ Page 指定,則預設值為 true 。 Visual Studio 會在建立程式碼後置檔案時自動包含 屬性。 針對以 C# 撰寫的 ASP.NET 頁,Visual Studio 會將 值設定為 true 。 針對 Visual Basic,Visual Studio 會將 值 false 設定為 ,因為處理常式是使用 Handles 關鍵字系結至事件,而 Visual Studio 會在產生事件處理常式時自動插入。 如果您設定 AutoEventWireuptrue ,則可以省略 (或移除 [控制碼 ] 關鍵字) 。

如果效能是關鍵考慮,請勿設定 AutoEventWireuptrue 為 。 啟用自動事件連線時,ASP.NET 必須在 15 到 30 之間嘗試比對事件與方法。

請注意下列關於將事件處理常式系結至事件:

  • 如果您設定 AutoEventWireuptrue ,請確定您不也會手動將頁面事件處理常式附加至事件。 如果您這樣做,可能會呼叫多個處理常式。

  • 自動系結只會針對頁面事件執行,不適用於頁面上控制項的事件。

  • 除了將事件系結至處理常式,您也可以覆寫 On 頁面或控制項的eventname方法。

適用於