本文介绍了什么叫Page_Load中,它是如何做到的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Page_Load不是虚方法。什么叫这种方法,它是如何做到的呢?是它反射或一些其它技术?也有多少事件是这样处理的?

Page_Load isn't a virtual method. What calls this method and how does it do it? Is it reflection or some other technique? Also how many events are handled this way?

也是它preferable来处理超载的OnLoad或Page_Load中的东西呢?他们如何不同?

Also is it preferable to handle things in an overloaded OnLoad or Page_Load? How are they different?

推荐答案

ASP.NET有一个称为AutoEventWireup功能 - 此功能允许您创建具有方法事件处理程序的Page_Load 和运行时会从父页面的事件连接起来的方法在你的类签名。基本上,运行时就这代表您:

ASP.NET has a feature called "AutoEventWireup" - this feature allows you to create methods that have the EventHandler signature with names like Page_Load and the runtime will wire up the event from the parent page to the method in your class. Basically the runtime does this on your behalf:

this.Load += this.Page_Load;

现在是最好禁用AutoEventWireup,要么自己在页面中创建这些事件处理程序的OnInit 方法或者干脆重写父页面的的OnLoad 方法。

Now it is better to disable AutoEventWireup and either create these event handlers yourself in the pages OnInit method or simply override the parent page's OnLoad method.

修改(响应下面的OP的评论):这个过程不涉及点击按钮等,但这个过程是相似的。

Edit (in response to the OP's comment below): This process doesn't cover button clicks and such but the process is similar.

为了像 MyButton_Click 的方法没有你明确地创建一个事件处理工作,你就必须设置的OnClick

In order for a method like MyButton_Click to work without you explicitly creating an event handler you would have to set the OnClick attribute on the control in the aspx file like this:

<asp:button 
    id="MyButton"
    onClick="MyButton_Click"
    runat="server" />

这将促使ASP.NET创建按钮单击委托你和它附加到按钮的点击事件。

This would prompt ASP.NET to create the button click delegate for you and attach it to the button's Click event.

这篇关于什么叫Page_Load中,它是如何做到的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:22