Asp. NET event model (well suited to learning articles) _ Practical Tips

Source: Internet
Author: User

The first line in the Default.aspx page is a page instruction:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Aspxeventsmodel._default"% >

Where the Codebehind property specifies the name of the code-behind page, inherits specifies the namespace and class to which it belongs, and the AutoEventWireup property is assignable to true and false, and the default is to true.
Let's first put two literal controls in the Default.aspx page:

Copy Code code as follows:

<asp:literal id= "Liinit" runat= "Server" ></asp:Literal>
<br/>
<asp:literal id= "Liload" runat= "Server" ></asp:Literal>

In the Default.aspx.cs page, the Page_Load method exists by default, defined as follows:

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
This. Liload.text = "This is on page load event";
}

The page is requested, and the method is executed when the page is loaded.

So why is this method executed when the page is loaded, and we do not register it with the corresponding event on the page.
There are also a lot of events on the page, and we list some important page events to look at:
The following event exists in the page class, which is the base class for the page:

Copy Code code as follows:

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, which is the base class for the page class:
Copy Code code as follows:

public event EventHandler DataBinding;
public event EventHandler disposed;
public event EventHandler Init;
public event EventHandler Load;
public event EventHandler PreRender;

We extract the events that are of interest to us and sort them according to the order in which they are executed:
PreInit: Raised at the beginning of the page initialization phase
Init: Raised when a page is initialized
InitComplete: Raised at the end of initialization of the page
Preload: Raised at the beginning of the load phase of the page
Load: Raised when a page is loaded
LoadComplete: Raised at end of load on page
PreRender: Raised when the page is about to be rendered

We see a lot of page events, mainly to study the init and load these two events. In the Default.aspx.cs page, add the Page_Init method, which is defined as follows:

Copy Code code as follows:

protected void Page_Init (object sender, EventArgs e)
{
This. Liinit.text = "This is the page init event";
}

So why not go back to the question above, and why do the Page_Init, Page_Load methods after the page request? The reason is:
Asp. NET autoeventwireup= "true" so that the page is bound to some special event methods to automatically recognize these methods with a specific name, without registering events.
These specific names include: Page_Init, Page_Load, etc. 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 autoeventwireup= "true" credit, when we autoeventwireup= "false" time, the request page again, Page_Init, Page_Load method is not executed. Then we can display the registration event:
Copy Code code as follows:

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 subclasses.
Look at ASP.net's comments as described in this way:
OnInit: An event that triggers a System.Web.UI.Control.Init.
OnLoad: An event that triggers a System.Web.UI.Control.Load.

Well, we're rewriting them here, and registering the event is reasonable.
We put the INIT registration event statement in the OnLoad method, put the load registration event statement into the OnInit method, is two processed content exchange to see what will be the result:

Copy Code code as follows:

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);
}

We requested page discovery: 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 means that the Init event has responded to completion, and then the method is registered with the Init event and will not be invoked. In the OnInit method, the Load event does not begin to trigger when the load registration event is valid for the load registration event. So now many times we have formed a specification that will only rewrite the OnInit method, not rewrite the OnLoad method, but also fulfill our needs. So there's no way to see the OnLoad method in some frames. We adjusted the Default.aspx.cs page to:
Copy Code code as follows:

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 don't feel the need for Page_Init and Page_Load, and we're adjusting again to:
Copy Code code as follows:

protected override void OnInit (EventArgs e)
{
This. Liinit.text = "This is the page init event";
This. Liload.text = "This is on page load event";
Base. OnInit (e);
}

I enclose the debug source default.aspx:
Copy Code code as follows:

<%@ 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 ">
<title></title>
<body>
<asp:literal id= "Liinit" runat= "Server" ></asp:Literal>
<br/>
<asp:literal id= "Liload" runat= "Server" ></asp:Literal>
</body>

Default.aspx.cs:
Copy Code code as follows:

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 on 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 on page load event";
//}
}
}


All right, let's try it yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.