B/S開發(二) ASP.NET Page指令之AutoEventWireUP

使用Visual Studio 2005開發一個ASP.NET Project時,在新增一個Web For http:// m時,IDE會自動在aspx文件的第一行產生Page指令

這次主要來說明一下Page屬性中AutoEventWireup指令的用法,一下是來自於MSDN官方文檔的說明(查看官方文檔):

說明:

獲取或設置一個值,該值指示 ASP.NET 頁的事件是否自動連接到事件處理函數。

如果 ASP.NET 頁的事件自動連接到事件處理函數,則爲 true;否則爲 false 默認爲 true。

備註:

當AutoEventWireup值爲true時,ASP.NET不需要你顯式的綁定事件處理程序跟頁面事件,比如Load事件。

當AutoEventWireup值爲False時,你必須顯示的綁定事件處理程序到一個方法。比如:如果你有爲一個頁面寫Page_load的方法代碼,僅當你寫成如下形式的示例代碼時,方法才能在Load時間被正確的調用(注意:在Visual Basic語法中用Handles關鍵字來聲明,在C#語法中用event handler聲明)

Visual Basic示例代碼如下:

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

C#示例代碼如下:

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_eventname來尋找其處理方法名,比如Page_Load或者Page_init。ASP.NET首先去檢查典型的事件方法簽名(也就是說,它特定的Object和EventArgs參數)。如果沒有找到帶此簽名的事件處理程序,則ASP.NET將檢查沒有參數的重載。
當AutoEventWireup被設置爲false時,你必須顯示的綁定事件處理方法跟事件,如下面的代碼所示。在這種情況下,方法名稱不必須遵循某一模式。

如果未在 @ Page 指令中指定 AutoEventWireup,默認值爲 true Visual Studio 在創建代碼隱藏文件時自動包括該特性。 對於以 C# 編寫的 ASP.NET 頁面,Visual Studio 將值設置爲 true 對於 Visual Basic Visual Studio,將值設置爲 false,因爲通過使用 Handles 關鍵字(該關鍵字在 Visual Studio 生成事件處理程序時由 Visual Studio 自動插入)將處理程序綁定到事件。 如果將 AutoEventWireup 設置爲 true,您可以省略(或刪除)Handles 關鍵字。
如果主要考慮性能,則不要將 AutoEventWireup 設置爲 true 在啓用自動事件連接時,ASP.NET 必須進行 15 到 30 次嘗試,使將事件與方法匹配。

注意下列有關將事件綁定事件處理程序的內容:


  • 如果將 AutoEventWireup 設置爲 true,請確保不會同時將頁事件處理程序手動附加到事件。 如果這樣做,則可能多次調用處理程序。
  • 只爲頁面事件執行自動綁定,而不爲頁面上的控件的事件執行。
  • 作爲將事件綁定至句柄的另一個選擇,可覆蓋頁面或控件的 Oneventname 方法。

示例

下面的代碼示例演示如何設置或讀取代碼中的 AutoEventWireup 屬性。(譯者備註:visual studio 2005中未測試成功)

Visual Basic 示例:

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

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

C#示例:

// GVisual Basic和C#的Page屬性的設置分別如下
et the current AutoEventWireup property value.
Console.WriteLine(
    "Current AutoEventWireup value: '{0}'",
    pagesSection.AutoEventWireup);

// Set the AutoEventWireup property to false.
pagesSection.AutoEventWireup = false;

VIsual Basic和C#的Page屬性的設置分別如下:

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

VB代碼示例:

' 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

C#代碼如下:

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

下面的示例演示如何在AutoEventWireup爲false時顯示連接事件。

Visual Basic:

' 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

C#:

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章