开发者

Would base.OnLoad() cause an infinite loop if called in page_load event?

A member of my team went through a few pages in our ASP.NET web application and changed some OnLoad overrides to page_load events, but he did not remove the call to base.OnLoad().

This:

Public void override OnLoad()
{
    //stuff
    base.OnLoad();
}

To this:

Public void Page_Load(object sender, EventArgs e)
{
    //stuff
    base.OnLoad();
}

Note: I apologize if there are syntax errors, I am not on a computer with the actual source code.

When we pushed out code to the live server we started having issues with the IIS app_pool crashing every 45 mins to an hour. We are still not entirely sure this was the issue but I am curious where page_load events get invoked from. Do they get invoked from the On开发者_开发知识库Load method in the system.web.ui.page? If so then I have the opinion this was causing an infinite loop and eventually running out of memory and crashing the app_pool.

Could this be the cause of our troubles?


base.OnLoad(); causes the Load event to be raised. That would then cause your page's OnLoad event handler to be raised, which contains the call to OnLoad again. The code you've posted is indeed incorrect.

The ASP.NET Page Life Cycle Overview article on MSDN is a good read for explaining how page loads and other events work.


Even if it doesn't cause infinite loop, the decision of change from overriding the base virtual method to handling events, is not an intelligent one. I would suggest you to override, instead of worrying about infinite loops. The fact that it makes you worry, is an indication that overriding is a better choice.

Read Item 30 : Prefer Overrides to Event Handlers from Effective C# by Bill Wagner.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜