PagesSection.AutoEventWireup 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示 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
下面的示例演示如何在何时AutoEventWireupfalse显式连接事件。
// 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
注解
当 AutoEventWireup 是 true时,ASP.NET 不需要将事件处理程序显式绑定到页面事件,例如 Load。
如果为AutoEventWireup,false则必须将事件显式绑定到方法。 例如,如果在页面的代码中有一个 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);
}
}
当 AutoEventWireup 是 true时,处理程序将根据事件的名称和签名自动绑定到事件。 对于每个事件,ASP.NET 搜索根据模式 Page_事件名称命名的方法,例如 Page_Load 或 Page_Init。 ASP.NET 首先检查具有典型事件处理程序签名(即指定 Object 和 EventArgs 参数)的重载。 如果未找到具有此签名的事件处理程序,ASP.NET 检查没有参数的重载。
如果为<
默认值是在trueAutoEventWireup指令中@ Page未指定。 创建代码隐藏文件时,Visual Studio 会自动包含该属性。 对于用 C# 编写的 ASP.NET 页,Visual Studio 将值设置为 true。 对于 Visual Basic,Visual Studio 将值 false 设置为因为处理程序通过使用 Handles 关键字绑定到事件,后者在生成事件处理程序时由 Visual Studio 自动插入。 如果设置为AutoEventWireuptrue,则可以省略句柄关键字(或删除)。
如果性能是一个关键考虑因素,请不要设置为AutoEventWireuptrue。 启用自动事件连接后,ASP.NET 必须在 15 到 30 次之间尝试将事件与方法匹配。
有关将事件处理程序绑定到事件,请注意以下事项:
如果设置为
/>,请确保不手动将页面事件处理程序附加到事件。 如果这样做,可能会多次调用处理程序。 自动绑定仅针对页面事件执行,而不适用于页面上控件的事件。
作为将事件绑定到处理程序的替代方法,可以替代
On页面或控件的 eventname 方法。