search
HomeBackend DevelopmentC#.Net TutorialWhat is the order in which Asp.NET page events are loaded?

This article mainly introduces to you the sequence of event loading in Asp.NET pages. The article introduces it in detail through pictures, texts and sample codes. It has certain reference and learning value for everyone. Friends who need it can follow it below. Come and learn with me.

This article mainly introduces to you the relevant content about the loading sequence of Asp.NET page events, and shares it for your reference and study. I won’t say much below, let’s take a look at the detailed introduction:

Events in ASP.NET master pages and content pages

We know that both master pages and content pages can contain event handlers for controls. For controls, controls in the content page raise events in the content page, and controls in the master page raise events in the master page. Control events are not sent from the content page to the master page, and events from master page controls cannot be handled in the content page, they are only handled within their own events.

The following is the sequence of events after the master page (Master) and the content page (ContentPage) are merged:


Master页面控件 Init 事件。
ContentPage页面控件 Init 事件。
Master页  Init 事件。
ContentPage页 Init 事件。
ContentPage页 Load 事件。
Master页  Load 事件。
ContentPage页面控件 Load 事件。
ContentPage页面 PreRender 事件。
Master页面  PreRender 事件。
Master页面控件 PreRender 事件。
ContentPage页面控件 PreRender 事件。

Master The order of events in layout and content pages is not important to the page developer. However, if you create event handlers that depend on the availability of certain events, it is helpful to understand the order of events in master pages and content pages.

The sequence of page event loading in Asp.Net

1. When a single Page is executed, events will be activated in the following order:


Page.PreInit
Page.Init
Page.InitComplite
Page.PreLoad
Page.Load
Page.LoadComplete
Page.PreRender
Page.PreRenderComplete

2. If the page inherits from another page, such as BasePage:System.Web.UI.Page, add a verification function to BasePage. For example, if you check whether you are logged in, whether you have permissions, etc., and other pages inherit from BasePage, the event activation sequence of BasePage and final Page is:


BasePage.PreInit
Page.PreInit
BasePage.Init
Page.Init
BasePage.InitComplite
Page.InitComplite
BasePage.PreLoad
Page.PreLoad
BasePage.Load
Page.Load
BasePage.LoadComplete
Page.LoadComplete
BasePage.PreRender
Page.PreRender
BasePage.PreRenderComplete
Page.PreRenderComplete

3. If you use MasterPage, the events in MasterPage and the events in ContentPage are activated in the following order:


ContentPage.PreInit
Master.Init
ContentPage.Init
ContentPage.InitComplite
ContentPage.PreLoad
ContentPage.Load
Master.Load
ContentPage.LoadComplete
ContentPage.PreRender
Master.PreRender
ContentPage.PreRenderComplete

It should be noted that there is no PreInit event in Master .

4. If ContentPage inherits BasePage, then the execution order of each event will become:


BasePage.PreInit
ContentPage.PreInit
Master.Init
BasePage.Init
ContentPage.Init
BasePage.InitComplite
ContentPage.InitComplite
BasePage.PreLoad
ContentPage.PreLoad
BasePage.Load
ContentPage.Load
Master.Load
BasePage.LoadComplete
ContentPage.LoadComplete
BasePage.PreRender
ContentPage.PreRender
Master.PreRender
BasePage.PreRenderComplete
ContentPage.PreRenderComplete

only Need to remember: load the inherited page first, and then load itself. If the inherited page has inheritance, load the inheritance of the inherited page first.

Init, Load, PreRender event execution sequence:

1) Init event of the control

2) Init event of the page where the control is located

3) Load event of the page where the control is located

4) Load event of the control

5) PreRender event of the page where the control is located

6) PreRender event of the control

Some insights from personal research: (The following two points can be verified by building a self-made page and rewriting related events)

1. The Init event is raised from the innermost control (such as user control) to the outermost control (page), and other events such as Load and PreRender are raised from the outermost control to the innermost control.

2. The execution sequence of the same events between controls is from left to right and top to bottom according to the position of the control on the page.

Note:

1. Remember that the user control is also considered a control in the page;

2. Looking at the user control as a separate special page, it itself and the controls it contains also follow the same rules.

3. If the onload event of the client body object is used in the client program (such as JavaScript), please note that this client event is executed last, that is, after all events on the server side have been executed. Just execute.

4. There are no PreInit and OnComplete events in Master and user-defined controls. Master itself is a user control usercontrol. Usercontrol inherits TemplateControl, and TemplateControl inherits Control. Let us see what can be rewritten in Control. event (partial screenshot), you can also view this official document: https://referencesource.microsoft.com/#System.Web/UI/Control.cs,87dbac93d9749fa2.


Event handler name Occurrence time
Page_Init In Web Form The view state loads the server control and initializes it. This is the first step in the form life cycle
Page_Load Loads the server control on the Page object. Since the view state information is available at this time, you can use code here to change the settings of the space or display text on the page.
Page_PreRender The application will render Page

Page_Unload

Page is unloaded from memory

Page_Error

An unhandled exception occurred

Page_AbortTransaction

Transaction terminated

##Page_CommitTransaction

Transaction Accepted

Page_DataBinding

Place the page on the server space Loaded together with the data source binding

Page_Disposed

The Page object is released from memory. This is the last event in the Page object life cycle

The above is the detailed content of What is the order in which Asp.NET page events are loaded?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C# .NET: Building Applications with the .NET EcosystemC# .NET: Building Applications with the .NET EcosystemApr 27, 2025 am 12:12 AM

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# as a Versatile .NET Language: Applications and ExamplesC# as a Versatile .NET Language: Applications and ExamplesApr 26, 2025 am 12:26 AM

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# .NET for Web, Desktop, and Mobile DevelopmentC# .NET for Web, Desktop, and Mobile DevelopmentApr 25, 2025 am 12:01 AM

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

C# .NET Ecosystem: Frameworks, Libraries, and ToolsC# .NET Ecosystem: Frameworks, Libraries, and ToolsApr 24, 2025 am 12:02 AM

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

Deploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideDeploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideApr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

Demystifying C# .NET: An Overview for BeginnersDemystifying C# .NET: An Overview for BeginnersApr 20, 2025 am 12:11 AM

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools