Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Conceptual Analysis of WebForm Lifecycle in Asp.Net

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the conceptual analysis of WebForm life cycle in Asp.Net, which is very detailed and has certain reference value. Friends who are interested must finish it!

I. the concept of Asp.Net page life cycle

When we enter the URL in the browser address bar and enter to view the page, we will send a request request to the server-side IIS), and the server will judge the request page sent. When the TTP page handler class is fully identified, the ASP.NET runtime will call the ProcessRequest method of the handler to process the request and create the page object. In general, there is no need to change the implementation of this method because it is provided by the Page class. Next, the ProcessRequest method of the created page object causes the page to go through various stages: initializing, loading view state information and postback data, loading the page's user code, and executing postback server-side events. After that, the page enters display mode: collecting the updated view state, generating the HTML code, and then sending the code to the output console. Finally, unload the page and assume that the request has been processed. The handling process of this series of events completed by the page object ProcessRequest method is the Asp.Net page life cycle.

Second, why do you need to understand the life cycle of Asp.Net pages

Because understanding the Asp.Net page life cycle can help developers write programs at the appropriate stage of the life cycle in order to achieve the desired results, and if you want to develop your own custom controls, you must be familiar with the page life cycle in order to properly initialize the control, populate the control's properties with view state data, and run any control behavior code. In other words, only when you are familiar with a series of events from creation to final uninstallation, the development will be smooth, and there will be no feeling in the clouds.

III. Life cycle stage

1. Request page: the page request occurs before the page life cycle begins.

2. Start: in the start phase, page properties, such as Request and Response, are set. At this stage, the page also determines whether the request is a postback request or a new request, and sets the IsPostBack property.

3. Initialize the page: during page initialization, you can use the controls in the page, and the UniqueID property of each control will be set. If the current request is a postback request, the postback data has not been loaded and the control property value has not been restored to the value in view state.

4. Load the page: during loading, if the current request is a postback request, the control properties are loaded with information recovered from view state and control state.

5. Validation: during validation, the Validate method of all validator controls is called, which sets the IsValid property of each validator control and page.

6. Postback event handling: if the request is a postback request, all event handlers are called.

7. Render the page: during page rendering, the view state is saved to the page, and then the page invokes each control to provide its rendered output to the OutputStream of the page's Response property.

8. Unload the page: unload is called when the page is fully rendered, sent to the client, and ready to be discarded. At this point, page properties, such as Response and Request, are unloaded and cleanup is performed.

IV. Life cycle events

1 、 PreInit

Use this event to do the following:

Check the IsPostBack property to determine if this is the first time the page has been processed.

Create or recreate dynamic controls.

Set the master page dynamically.

Set the Theme property dynamically.

Reads or sets the value of the profile property.

Note:

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

2 、 Init

Raised after all controls have been initialized and all appearance settings have been applied. Use this event to read or initialize control properties.

3 、 InitComplete

Raised by a Page object. Use this event to handle tasks that require all initialization work to be completed first.

4 、 PreLoad

Use this event if you need to perform processing on the page or control before the Load event.

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

5 、 Load

Page calls the OnLoad event method on Page, and then recursively does the same for each child control, and so on, until the page and all controls are loaded.

Use the OnLoad event method to set properties in the control and establish a database connection

6. Control event

Use these events to handle specific control events, such as the Click event of the Button control or the TextChanged event of the TextBox control.

Note:

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

7 、 LoadComplete

Use this event for tasks that need to load all other controls on the page.

8 、 PreRender

Before the event:

The Page object EnsureChildControls for each control and page.

The DataBind method is called for each data-bound control that has the DataSourceID property set. For more information, see the following data-bound events for data-bound controls.

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

9 、 SaveStateComplete

Before this event occurs, ViewState has been saved for the page and all controls. Any changes made to the page or control at this time are ignored.

Use this event to perform tasks that require that view state has been saved but no changes have been made to the control.

10 、 Render

This is not an event; at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method for writing out the control tags that are sent to the browser.

If you create a custom control, you usually override this method to output the markup for the control. However, if custom controls merge only standard ASP.NET Web server controls, not 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 and renders, so there is no need to explicitly render the control in code.

11 、 Unload

This event occurs first for each control and then for the page. In a control, use this event to perform a final cleanup on a specific control, such as closing a control-specific database connection.

For the page itself, use this event to perform final cleanup tasks, such as closing open file and database connections, or completing logging or other request-specific tasks.

Be careful

During the uninstall phase, the page and its controls have been rendered, so no further changes can be made to the response stream. If you try to call a method, such as the Response.Write method, the page throws an exception.

These are all the contents of the article "Conceptual Analysis of the WebForm Lifecycle in Asp.Net". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report