When we create an ASP. NET application, a Default. aspx and Default. aspx. cs page are generated by Default.
On the Default. aspx page, the first line is a page command:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "AspxEventsModel. _ Default" %>
Here, the CodeBehind attribute specifies the name of the Code hiding page, and the Inherits specifies the namespace and class to which it belongs. The AutoEventWireup attribute can be assigned true or false, and the default value is true.
We first put two Literal controls in the Default. aspx page:
<Asp: Literal ID = "LiInit" runat = "server"> </asp: Literal>
<Br/>
<Asp: Literal ID = "LiLoad" runat = "server"> </asp: Literal>
The Default. aspx. cs page contains the Page_Load method by Default, which is defined as follows:
Protected void Page_Load (object sender, EventArgs e)
{
This. LiLoad. Text = "this is the page Load event ";
}
The page is requested. This method is executed when the page is loaded.
Why is this method executed during page loading? We didn't register it with the corresponding event on the page.
There are also many page events. Let's take a look at some important page events:
The following events exist in the Page class. The Page class is the base class of the Page:
Public event EventHandler InitComplete;
Public event EventHandler LoadComplete;
Public event EventHandler PreInit;
Public event EventHandler PreLoad;
Public event EventHandler PreRenderComplete;
Public event EventHandler SaveStateComplete;
The following event exists in the Control class. The Control class is the base class of the Page class:
Public event EventHandler DataBinding;
Public event EventHandler Disposed;
Public event EventHandler Init;
Public event EventHandler Load;
Public event EventHandler PreRender;
We extract the events of interest and sort the events according to their execution order:
PreInit: triggered at the beginning of page Initialization
Init: triggered during page Initialization
InitComplete: triggered at the end of page Initialization
PreLoad: triggered at the beginning of the page loading phase
Load: triggered during page loading
LoadComplete: triggered at the end of page loading
PreRender: triggered when the page is about to be rendered
We can see that there are many page events, mainly to study the Init and Load events. On the Default. aspx. cs page, add the Page_Init method, which is defined as follows:
Protected void Page_Init (object sender, EventArgs e)
{
This. LiInit. Text = "this is the page Init event ";
}
Back to the above question, why does the Page_Init and Page_Load methods be executed after the page request? The reason is:
In ASP. NET, AutoEventWireup = "true" binds the page to some special event methods, automatically identifies these methods with specific names, and does not need to register events.
These specific names include Page_Init and Page_Load. The Init event is automatically bound to the Page_Init method, and the Load event is automatically bound to the Page_Load method. Of course, these are the merits of AutoEventWireup = "true". When we request the page again when AutoEventWireup = "false", the Page_Init and Page_Load methods will not be executed. Then we can display the registration event:
Protected override void OnInit (EventArgs e)
{
This. Init + = new EventHandler (this. Page_Init );
Base. OnInit (e );
}
Protected override void OnLoad (EventArgs e)
{
This. Load + = new EventHandler (this. Page_Load );
Base. OnLoad (e );
}
So what is OnInit and OnLoad?
They are virtual methods defined in the Control class, so we can rewrite them in its subclass.
According to ASP. NET annotations, the descriptions are as follows:
OnInit: triggers the System. Web. UI. Control. Init event.
OnLoad: triggers the System. Web. UI. Control. Load event.
Well, we can rewrite them here and register the event reasonably.
We put the event registration statement for Init In the OnLoad method, and put the event registration statement for Load in the OnInit method. It is the exchange of the two processed content to see what the results will be:
Protected override void OnInit (EventArgs e)
{
This. Load + = new EventHandler (this. Page_Load );
Base. OnInit (e );
}
Protected override void OnLoad (EventArgs e)
{
This. Init + = new EventHandler (this. Page_Init );
Base. OnLoad (e );
}
On the request page, we found that the Load registration event was executed in the OnInit method, and the Init registration event was not executed in the OnLoad method. The reason is that after the OnInit method is executed, it indicates that the Init event response has been completed, and the method is registered for the Init event later, and will not be called. When the OnInit method registers an event for Load, the Load event does not start to be triggered, which is valid for the Load registration event. So now we often have a specification that only overrides the OnInit method and does not overwrite the OnLoad method to meet our needs. Therefore, the OnLoad method is not seen in some frameworks. We adjusted the Default. aspx. cs page:
Protected override void OnInit (EventArgs e)
{
This. Init + = new EventHandler (this. Page_Init );
This. Load + = new EventHandler (this. Page_Load );
Base. OnInit (e );
}
Obviously, we do not need the Page_Init and Page_Load methods. We can adjust them:
Protected override void OnInit (EventArgs e)
{
This. LiInit. Text = "this is the page Init event ";
This. LiLoad. Text = "this is the page Load event ";
Base. OnInit (e );
}
I have attached the debugging source code Default. aspx:
View Code
<% @ Page Language = "C #" AutoEventWireup = "false" CodeBehind = "Default. aspx. cs" Inherits = "AspxEventsModel. _ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Asp: Literal ID = "LiInit" runat = "server"> </asp: Literal>
<Br/>
<Asp: Literal ID = "LiLoad" runat = "server"> </asp: Literal>
</Body>
</Html>
Default. aspx. cs:
View Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Namespace AspxEventsModel
{
Public partial class _ Default: System. Web. UI. Page
{
Protected override void OnInit (EventArgs e)
{
This. LiInit. Text = "this is the page Init event ";
This. LiLoad. Text = "this is the page Load event ";
// This. Init + = new EventHandler (this. Page_Init );
// This. Load + = new EventHandler (this. Page_Load );
Base. OnInit (e );
}
// Protected override void OnLoad (EventArgs e)
//{
// This. Load + = new EventHandler (this. Page_Load );
/// This. Init + = new EventHandler (this. Page_Init );
// Base. OnLoad (e );
//}
// Protected void Page_Init (object sender, EventArgs e)
//{
// This. LiInit. Text = "this is the page Init event ";
//}
// Protected void Page_Load (object sender, EventArgs e)
//{
// This. LiLoad. Text = "this is the Load event on the page ";
//}
}
}
Now, try it on your own.
From the kiss of the eight gods