ASP. NET's event model of is well suited to learning articles

  • 2020-05-19 04:28:34
  • OfStack

In the Default.aspx page line 1 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-hidden page, Inherits specifies the namespace and class to which it belongs, AutoEventWireup property can be assigned to true and false, and true by default.
We start by putting two Literal controls on the Default.aspx page:


        <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 will exist by default, as defined below:


        protected void Page_Load(object sender, EventArgs e)
        {
            this.LiLoad.Text = " This is on the page Load The event ";
        }

The page is requested and the method is executed when the page loads.

So why is this method executed when the page loads? We didn't register it with the corresponding event of the page.
There are also many page events. Let's list 1 important page events.
There are the following events in the Page class, which 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;

There are the following events in the Control class, which 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 most concerned events and sort them according to the order of execution:
PreInit: raised at the beginning of the initialization phase of the page
Init: raised at page initialization
InitComplete: raised at the end of page initialization
PreLoad: raised at the beginning of the page's load phase
Load: raised when the page is loaded
LoadComplete: raised at the end of page loading
PreRender: raised when the page is about to be rendered

We have seen many page events, mainly to study Init and Load. 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 on the page Init The event ";
        }

So back to the question above, why are Page_Init and Page_Load methods executed after the page request? The reason is:
ASP.NET, AutoEventWireup="true", binds pages to special event methods that automatically identify those methods with specific names without registering events.
These specific names include: Page_Init, Page_Load, and so on. Here's why: 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, this is all due to AutoEventWireup="true", when we AutoEventWireup="false", when we request the page again, Page_Init, 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);
        }

What about OnInit and OnLoad?
They are virtual methods defined in the Control class, so we can override them in its subclasses.
Look at the comments on ASP.NET goes like this:
OnInit: trigger System Web. UI. Control. Init events.
OnLoad: trigger System Web. UI. Control. Load events.

Good. It makes sense for us to rewrite them here and register events.
We put the Init registration event statement into the OnLoad method, and the Load registration event statement into the OnInit method, which is the content exchange between the two processes. Let's see what happens:


        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 that the Load registration event be executed in the OnInit method and not in the Init registration event in the OnLoad method. The reason is that after the OnInit method is executed, it means that the Init event has been responded to, and then the Init event is registered with the Init event and the method is not called. When the OnInit method registers the Load event, the Load event does not start to be triggered, which is valid for the Load registration event. So many times now, we have formed a specification that only overrides the OnInit method, but not the OnLoad method, to meet our requirements. So you don't see in some frameworks that this is how the OnLoad method is formed. We changed the Default.aspx.cs page to:

        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 think Page_Init and Page_Load methods are needed. We adjust them to:

        protected override void OnInit(EventArgs e)
        {
            this.LiInit.Text = " This is on the page Init The event ";
            this.LiLoad.Text = " This is on the page Load The event ";
            base.OnInit(e);
        }

I attach the source code for debugging Default.aspx:

<%@ 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:

        protected void Page_Load(object sender, EventArgs e)
        {
            this.LiLoad.Text = " This is on the page Load The event ";
        }
0
Well, try it yourself.


Related articles: