开发者

why base.onload(e) needed for custom page base class

开发者 https://www.devze.com 2023-02-20 02:59 出处:网络
I have a page base class (.NET4): public class SitePageBase : System.Web.UI.Page { protected override void OnLoad(EventArgs e)

I have a page base class (.NET4):

public class SitePageBase : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }
}

And derived class

开发者_高级运维
public partial class WebsitePage : SitePageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // some processing
    }
}

I checked the "Page_Load" in derived class only works if the "base.OnLoad(e)" is there on the base class's OnLoad event handler. Otherwise the "Page_Load" in derived class not get fired at all.

Can any one tell me, why this is happening?

P.S. The AutoEventWireup is set to true.


Because you are overriding the behaviour of the base class, and if you don't call base.OnLoad(e) then the base class will not continue it's implementation (in this case to raise the Load event)

In your case, if your not doing anything in OnLoad you can remove the method all together, or as i do remove Page_Load and do your load login in the overridden OnLoad

These are worth a read

  • onload vs page load
  • When creating a web control should you override OnLoad or implement Page_Load


Clearly it's the base's OnLoad function that raises the Load event. If you don't call the base's OnLoad function, the Load event doesn't get raised, and your Page_Load event handler never gets invoked.


Because the base implementation of the OnLoad method fires the event that is handled by Page_Load


Typically the base version of an On<Event> looks a little bit like this:

protected virtual void OnLoad(EventArgs e)
{
   if(Loaded != null)
      Loaded(this, e);
}

So in other words, the base member is the one that actually fires the event.


set the AutoEventWireup to false, that solved the problem for me :)

0

精彩评论

暂无评论...
验证码 换一张
取 消