asp.net page life cycle Detailed _ practical skills

Source: Internet
Author: User
Tags html form unique id

ASP.net is an integral part of the Microsoft. NET strategy. It has a great development relative to the previous ASP, and introduces many new mechanisms. This article on the life cycle of asp.net page to make a preliminary introduction, in order to be able to play to guide everyone better and more flexible to manipulate the role of asp.net. When a request for a Web page (which may be done by a user or through a hyperlink) is sent to the Web server, the page then runs through a series of events from creation to processing. When we try to build the ASP.net page, this execution cycle is not necessary to consider, it will only ask for trouble. However, if manipulated correctly, the execution cycle of a page will be an effective and powerful tool. Many developers, when writing asp.net pages and user controls, find that knowing what's going on in the process and when it happens will play an important role in helping complete the task. Now let me introduce you to a asp.net page. 10 events from creation to process completion. Also, show you how to add your own code to these events to achieve a predetermined effect.

A Initializing objects
A page's controls (and the page itself) should initially be properly initialized. By making a list of all the objects in your C # file's constructor (Figure 1), the page knows how many objects to create and their types. Once you have a reputation for all the objects in your constructor, you can access them by inheriting classes, methods, events, or attributes. However, if some of your objects are some of the controls specified in the ASPX file, then the controls have no properties to speak of. Also, accessing them through code can cause some unexpected errors because these control instances are not in a certain order of creation (if they were created together). Also, you can overload initialization events by OnInit.

Two Import ViewState data
After the event is initialized, all controls can be accessed only through their IDs (since no corresponding DOM is available). In LoadViewState this event, all controls will get their first property: the ViewState property. This property will eventually be returned to the server to determine whether the page has been accessed by the user or is still being accessed by the user. The ViewState property is saved as a string of "name/value" pairs, which contains information such as the control's text and values. This property is stored in the Value property of a hidden <input> control and is passed when the page is requested. This way than the Asp3.0 to maintain, judge the way the state of the page has made great progress ah. Also, you can overload the LoadViewState event function to set the value of the corresponding control.

Three Processing postback data with LoadPostData
At this stage of the page creation, the server handles the form data that is submitted by the control on the page (called postback data in asp.net). When a page submits a form, the framework performs a IPostBackDataHandler interface operation on each control that submits the data. The page then executes the LoadPostData event, parses the page, finds each control that performs the IPostBackDataHandler interface operation, and updates the control state with the appropriate postback data. asp.net is done by matching the name/value pair in the Namevalue set with the unique ID of each control. Therefore, each control must have a unique ID on the asp.net page, and there can be no instances where several controls have a common ID. Even for user-defined controls, the framework gives them unique IDs. After the LoadPostData event, the following Raisepostdatachanged event will be executed.

Four Import objects
In the Load event, the object is instantiated. All objects are first laid out in the DOM page (called the Control tree in asp.net) and can be referenced by code or relative location. This allows an object to easily obtain property values such as width, height, value, visibility, and so on in HTML from the client. In the Load event, there is, of course, the occurrence of actions such as setting control properties. This process is the most important and major part of the entire lifecycle, and you can overload the load event by calling OnLoad.

Five Raisepostbackchanged Events
As mentioned above, this event occurs after all controls perform the IPostBackDataHandler interface operation and are updated with the correct postback data. In this process, each control is given a Boolean value to flag that the control has not been updated. ASP.net then searches the entire page for any controls that have been updated and performs raisepostdatachanged event operations. However, this event occurs only after all controls have been updated and the Load event is complete. This ensures that a control is not manually changed in the Raisepostdatachanged event until it is updated by the postback data.

Six Handling Client Postback Events
When the event caused by the postback data on the server is complete, the object that produces the postback data performs the RaisePostBackEvent event operation. However, this can happen because a control state changes so that it returns the form to the server or the user clicks the Submit button to return the form to the server. In this case, there should be a corresponding processing code to reflect the event-driven object-oriented (OOP) programming principle. Due to the accuracy requirements of the data presented to the browser, the RaisePostBackEvent event is the last occurrence in a series of postback events.
Controls that are changed during the postback process should not be updated after the function function is invoked. That is, any data that changes as a result of an expected event should be reflected on the final page. You can modify the RaisePostBackEvent function to meet your requirements,

Seven Pre-submitted objects
The last moment that can change the object and save the change is this step--presenting the object in advance. In this way, you can make a final change to the control's properties, control tree structure, and so on. At the same time, there is no need to consider asp.net to make any changes, because at this time has been separated from the database calls and ViewState update. After this step, all modifications to the object will eventually be determined and cannot be saved to the ViewState of the page. You can overload this step by OnPreRender.

Eight Save ViewState
ViewState is saved after all modifications to the page control have been completed. The state data for the image remains in the hidden <input> control, and the object state data presented to the HTML is also obtained from here. In the SaveViewState event, the value can be saved to the ViewState object, but the changes on the controls on the page are not available. You can overload this step with a saveviewstate.

Nine Presenting to HTML
The render event occurs when you use HTML to create a page that is output to a browser. During the render event, the page calls the objects in which they are presented to the HTML. The page can then be accessed by the user's browser in HTML form. When the render event is overloaded, the developer can write custom HTML code to invalidate the previously generated HTML and organize the page according to the new HTML. The Render method takes a HtmlTextWriter object as a parameter and uses it to display HTML as a Web page on the browser. You can still do some modifications, but they are just a few changes to the client. You can overload the render event

Ten Destroying objects
After presentation to HTML, all objects should be destroyed. In the Dispose event, you should destroy all objects created when the page was built. At this point, all the processing is complete, so destroying any remaining objects will not produce errors, including page objects. You can overload the Dispose event

The

full text summary
is more than 10 events in the life cycle of the ASP.net page. Every time we ask for a asp.net page, we all go through the same process: from initializing the object to destroying the object. By understanding the internal operating mechanism of the ASP.net page, I believe that you will be more comfortable when you write and debug your code.

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.