What calls Page_Load and how does it do it?

后端 未结 5 1559
眼角桃花
眼角桃花 2020-12-15 17:17

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?

相关标签:
5条回答
  • 2020-12-15 17:44

    In the page directive it says: Autoeventwireup="true"

    Thats what happens, its automatically wired up to the Load event... (and some other events like PreInit, Init, Unload etc.)

    0 讨论(0)
  • 2020-12-15 17:57

    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;
    

    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.

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

    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" />
    

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

    0 讨论(0)
  • 2020-12-15 17:57

    The order in which the virtual methods (OnLoad) and event handlers (Page_Load) are called is defined by the so called page lifecycle. This is just the way how the ASP.NET runtime processes an incoming request (e.g. with the Init, Load, Render stages).

    You can use either OnLoad or Page_Load but you have to be aware of what happens:

    • inside OnLoad you must call base.OnLoad
    • inside base.OnLoad the Load event will be raised
    • Page_Load is a handler for the Load event (which is automatically wired-up) and will therefore be invoked as a result of the Load event that's being raised.

    If you do not call base.OnLoad in your OnLoad override, then the Load event will not be raised.


    Update: you can use an empty page with the following code-behind to see what happens:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        base.Load += new EventHandler(My_Page_Load);
    }
    
    void My_Page_Load(object sender, EventArgs e)
    {
        Response.Write("My_Page_Load<br/>");
    }
    
    protected override void OnLoad(EventArgs e)
    {
        Response.Write("Start of OnLoad<br/>");
        base.OnLoad(e);
        Response.Write("End of OnLoad<br/>");
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Page_Load<br/>");
    }
    

    Try commenting the base.OnLoad(e) call and see again.

    0 讨论(0)
  • 2020-12-15 17:59

    Take a look at the ASP.NET page lifecycle, there is a section for lifecycle events where it describes load.

    Load
    The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded. Use the OnLoad event method to set properties in controls and establish database connections.

    Further Quote:

    Note that when creating an event handler using the Page_event syntax, the base implementation is implicitly called and therefore you do not need to call it in your method. For example, the base page class's OnLoad method is always called, whether you create a Page_Load method or not. However, if you override the page OnLoad method with the override keyword (Overrides in Visual Basic), you must explicitly call the base method. For example, if you override the OnLoad method on the page, you must call base.Load (MyBase.Load in Visual Basic) in order for the base implementation to be run.

    0 讨论(0)
  • 2020-12-15 18:06

    The OnLoad method somewhere up the Page hierarchy calls the events assigned to Load (via +=).

    The naming Page_Load is just a convention. In AutoEventWireUp mode (i.e. no event handler explicitly declared) this convention is used to find event handlers by their names.

    If you have .Net1 available, you can see how the designer adds code to the page's OnInit() to add all components of the page and set

    this.Load += new System.EventHandler(this.Page_Load);
    

    .Net2 still does that, but in a separate file which is hidden somewhere under the Windows\Microsoft.Net\Framework\v*\Temporary ASP.Net Files.

    I find this chart on ASP.Net Page Life Cycle very useful.

    0 讨论(0)
提交回复
热议问题