共用方式為


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,會自動附加於頁面事件的兩種方法簽名形式。

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

備註

AutoEventWireuptrue時,ASP.NET 並不要求你明確將事件處理程序綁定到像 Load這樣的頁面事件。

AutoEventWireupfalse時,你必須明確將事件綁定到某個方法。 例如,如果你在某頁程式碼中有 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);
    }
}

AutoEventWireuptrue時,處理器會根據其名稱與簽名自動綁定於執行時的事件。 對於每個事件,ASP.NET 搜尋一個依照事件 Page_名稱命名的方法,例如 Page_LoadPage_Init或 。 ASP.NET 首先檢查具有典型事件處理簽名(即指定 Object 參數) EventArgs 的超載。 如果找不到帶有此簽名的事件處理器,ASP.NET 會檢查是否有無參數的超載。

AutoEventWireupfalse時,你必須明確將事件處理器綁定到事件,如前述範例所示。 在這種情況下,方法名稱不必遵循特定的模式。

預設值為 如果trueAutoEventWireup指令中未指定@ Page。 Visual Studio 在建立程式碼後方檔案時,會自動包含該屬性。 對於用 C# 撰寫的 ASP.NET 頁面,Visual Studio 會將值設定為 true。 對於 Visual Basic,Visual Studio 將值設為 , false 因為處理器透過 Handles 關鍵字綁定事件,Visual Studio 在產生事件處理器時會自動插入該關鍵字。 如果你設 AutoEventWireuptrue,可以省略(或移除) Handles 關鍵字。

如果效能是關鍵考量,就不要設定AutoEventWireuptrue 啟用自動事件連線時,ASP.NET 必須嘗試 15 到 30 次,才能將事件與方法匹配。

請注意以下關於綁定事件處理程序與事件的說明:

  • 如果你設定 AutoEventWireuptrue,請確保你不要手動將頁面事件處理程式附加到事件上。 如果有,牽繩人可能會被叫來不只一次。

  • 自動綁定僅對頁面事件執行,不對頁面控制項事件進行。

  • 作為綁定事件到處理器的替代方案,你可以覆寫On頁面或控制項的事件名稱方法。

適用於