简体   繁体   中英

ASP.net Page_Load method logic

I'm looking to gain an overall, high-level understand of what exactly happens when you put logic in the Page_Load method from an OOP viewpoint.

Code as follows:

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
         Label1.Text = "Hello";
    }
}

I think I have a solid understanding, but would love for somebody to confirm my observations and point out any errors or false assumptions that I'm making.

So from what I understand,

  • the _Default class has public accessibility and inherits from System.Web.UI.Page

  • this is a partial class, so this is only part of the implementation, the other part is located elsewhere

  • the Page_Load method returns void and is protected, meaning that any other method in the _Default class can access it and any derived classes can also access it

  • sender and e presumably come from the caller of the method, which is part of the page lifecycle somewhere..

Other than these observations, I have a question or two:

Is there no other implementation for Page_Load anywhere?

What is the OOP logic behind Page_Load being protected?

What initiates Page_Load?

Page_Load could be called anything, it's just being subscribed to the Load event of the Control class. It's usually defined only once, but if you wanted to you could put some more handlers onto it, just like with any event.

Similarly, the protected status of Page_Load doesn't mean a lot. It could really be defined anywhere; it could be private or public. It's an event handler and is therefore wired up to the invocation list for an event.

And for your third question, which you probably answered yourself by now, it's an event. Invocation of every subscribed handler happens when the server fires the Load event, when a user requests the page.

notice that the partial class inherits from System.Web.UI.Page. some good info to dig into there.

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