Page execution order in C #

Source: Internet
Author: User

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

public partial class _default:page
{
protected void Page_Load (object sender, EventArgs e)
{

}

#region Onpreinit First Step
protected override void Onpreinit (EventArgs e)
{
Check the IsPostBack property to determine whether the page is being processed for the first time.

Creates or re-creates a dynamic control.

Dynamically set the master page.

Dynamically sets the Theme property.

Reads or sets the profile property value.

Attention
If the request is a postback request, the value of the control has not been restored from view state. If a control property is set at this stage, its value may be overridden in the next event.

Base. Onpreinit (e);
}
#endregion

#region The second step of OnInit
protected override void OnInit (EventArgs e)
{
Raised after all controls have been initialized and all skin settings have been applied. Use this event to read or initialize the properties of a control.
Base. OnInit (e);
}
#endregion

#region Oninitcomplete Step Three
protected override void Oninitcomplete (EventArgs e)
{
Raised by the Page object. Use this event to handle tasks that require that all initialization work be completed first.

Base. Oninitcomplete (e);
}
#endregion

#region Preload Fourth Step
protected override void Onpreload (EventArgs e)
{
Use this event if you need to perform processing on a page or control before the Load event.

After the Page raises the event, it loads the view state for itself and all the controls, and then processes any postback data that is included in the Request instance.

Base. Onpreload (e);
}
#endregion

#region OnLoad Fifth Step
protected override void OnLoad (EventArgs e)
{
The page increases the OnLoad event method on page and then recursively performs the same action on each child control, so that it repeats until the page and all controls are loaded.
Use the OnLoad event method to set the properties in the control and establish a database connection.

Base. OnLoad (e);
}
#endregion

Sixth Step #region Control event
protected void Button1_Click (object sender, EventArgs e)
{
Use these events to handle specific control events, such as the Click event of a Button control or the TextChanged event of a TextBox control.

Attention
In a postback request, if the page contains a validator control, check the IsValid property of the page and individual validation controls before performing any processing.

}
#endregion

#region Onloadcomplete Seventh Step
protected override void Onloadcomplete (EventArgs e)
{
Use this event for a task that needs to load all other controls on the page.

Base. Onloadcomplete (e);
}
#endregion

#region OnPreRender Eighth Step
protected override void OnPreRender (EventArgs e)
{
Before the event occurs:

The Page object calls EnsureChildControls for each control and page.

Each data-bound control that has the DataSourceID property set will call the DataBind method. For more information, see Data-binding events for data-bound controls below.

The PreRender event occurs for each control on the page. Use this event to make a final change to the contents of a page or its controls.

Base. OnPreRender (e);
}
#endregion

#region Savestatecomplete nineth Step
protected override void Onsavestatecomplete (EventArgs e)
{
ViewState has been saved for the page and all controls before the event occurred. Any changes made to the page or control at this time will be ignored.

Use this event to perform tasks that require that view state be saved, but that no changes are made to the control.

Base. Onsavestatecomplete (e);
}
#endregion

#region Render Step Tenth
Render
This is not an event; At this stage of processing, the Page object will be called on each control with this method. All ASP. NET Web server controls have a Render method that is used to write the control tokens that are sent to the browser.

If you create a custom control, you typically override this method to output the markup for the control. However, if your custom control merges only standard ASP. NET Web server controls and does not merge custom tags, you do not need to override the Render method. For more information, see Developing Custom ASP. NET server controls.

The user control (. ascx file) automatically merges the rendering, so you do not need to explicitly render the control in your code.

#endregion

#region OnUnload 11th Step
protected override void OnUnload (EventArgs e)
{
The event occurs first for each control, and then for that page. In a control, use this event to perform a final cleanup of a specific control, such as closing a control-specific database connection.

For the page itself, use this event to perform the final cleanup work, such as closing open files and database connections, or completing logging or other requests for specific tasks.

Attention
During the unload phase, the page and its controls are rendered so that no further changes can be made to the response stream. If you try to invoke a method, such as the Response.Write method, the page throws an exception.

Base. OnUnload (e);
}
#endregion
}

When the page is postback, such as clicking the button, the above events will be executed again, and the Order of execution is:

1. Onpreinit
2. OnInit
3. Oninitcomplete
4. Onpreload
5. Page_Load
6. OnLoad

7. Button_Click
8. Onloadcomplete
9. OnPreRender

As you can see, the Button_Click event is executed after onload and can be tested:

public partial class TestControls:System.Web.UI.Page
{
static int count = 0;
protected void Page_Load (object sender, EventArgs e)
{
Response.Write (count "Page_Load <br/>");
Count;
}
protected override void Onpreinit (EventArgs e)
{
Base. Onpreinit (e);
Response.Write (count "Onpreinit <br/>");
Count;
}
protected override void OnInit (EventArgs e)
{
Base. OnInit (e);
Response.Write (count "OnInit <br/>");
Count;
}
protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e);
Response.Write (count "OnLoad <br/>");
Count;
}
protected override void Onpreload (EventArgs e)
{
Base. Onpreload (e);
Response.Write (count "Onpreload <br/>");
Count;
}
protected override void Onloadcomplete (EventArgs e)
{
Base. Onloadcomplete (e);
Response.Write (count "Onloadcomplete <br/>");
Count;
}
protected override void Oninitcomplete (EventArgs e)
{
Base. Oninitcomplete (e);
Response.Write (count "Oninitcomplete <br/>");
Count;
}
protected override void OnUnload (EventArgs e)
{
Base. OnUnload (e);
}
protected override void OnDataBinding (EventArgs e)
{
Base. OnDataBinding (e);
Response.Write (count "ondatabinding <br/>");
Count;
}
protected override void OnPreRender (EventArgs e)
{
Base. OnPreRender (e);
Response.Write (count "OnPreRender <br/>");
Count;
}

protected void Btngraphics_click (object sender, EventArgs e)
{
Bitmap bmp = New Bitmap (10, 10);
Graphics g = graphics.fromimage (BMP);
Response.Write (count "Btngraphics_click <br/>");
Count;
}
}

Page execution order in C #

Related Article

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.