简体   繁体   中英

Manually adding page event handlers in ASP.Net C#

When I've built applications in the past I've used AutoEventWireup to handle the page events for me. From what I've read this incurs a significant performance cost and I'd like to do it manually in my current application.

What is the correct place to set up the event handlers?

My initial thought was to just set up a constructor in my code behind file and do it there but I'm assuming that the auto generated portion of the partial class already contains a constructor that I'd be overriding.

I'm sorry to ask here on such a simple question. It seems like this should be easily searchable but I'm just not finding the answer I need. Thanks in advance for the help.

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    Load += new EventHandler(Page_Load);
}

For controls it's OnInit , since they have no OnPreInit . To be honest I've used OnInit for pages as well in the past :)

Of course, you could just do the above for all the events you need for your page, and define no event handlers whatsoever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM