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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值