In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the asp.net page life cycle", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to understand the asp.net page life cycle" bar!
Asp.net is an integral part of Microsoft's .net strategy. Compared with the previous Asp, it has a great development and introduces many new mechanisms. This article gives you a preliminary introduction to the life cycle of Asp.net pages, in order to guide you to manipulate Asp.net better and more flexibly. When a request to get a web page (either submitted by the user or via a hyperlink) is sent to the Web server, the page then runs a series of events from creation to processing. This execution cycle doesn't have to be taken into account when we try to build an Asp.net page, it's just asking for trouble. However, if manipulated correctly, the execution cycle of a page can be an effective and powerful tool. When writing Asp.net pages and user controls, many developers find it important to know what happened and when it happened.
I. initialize the object
The controls for a page (and the page itself) should initially be initialized correctly. By naming all objects in the constructor of your C# file (figure 1), the page knows how many objects to create and their types. Once you have named all the objects in your constructor, you can access them by inheriting classes, methods, events, or properties. However, if some of your objects are controls specified in the Aspx file, then these controls have no properties to speak of. At the same time, accessing them through code can cause some unexpected errors because these control instances do not have a definite order in which to create them (if they are created together). Also, you can overload initialization events through OnInit.
2. Import Viewstate data
After initializing the event, all controls can only be accessed by reference through their ID (because there is no corresponding DOM available yet). In the LoadViewState event, all controls 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, and contains information such as the text and value of the control. This property is stored in the value property of a hidden control and is passed when the page is requested. This way compared with the maintenance of Asp3.0, the way to judge 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.
3. Use LoadPostData to process Postback data
At this stage of page creation, the server processes the form data (called postback data in Asp.net) submitted by the controls on the page. When a page submits a form, the framework performs an 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 does this by matching the name / value pairs in the NameValue set to the unique ID of each control. Therefore, each control must have a unique ID on the Asp.net page, not in the case that several controls share an ID. Even some user-defined controls, the framework will give them their own unique ID. After the LoadPostData event, the following RaisePostDataChanged event is executed.
Four. import object
In the Load event, the objects are instantiated. For the first time, all objects are placed in a DOM page (called a control tree in Asp.net) and can be referenced through code or related locations. In this way, the object can easily obtain the property values in Html, such as width, height, value, visibility, and so on, from the client. In the Load event, of course, there are operations such as setting control properties. This process is the most important and important in the whole life cycle, and you can overload the Load event by calling OnLoad
Five. RaisePostBackChanged event
As mentioned above, this event occurs after all controls perform IPostBackDataHandler interface operations and are updated with the correct postback data. In the process, each control is assigned a Boolean value to indicate whether the control has been updated. Asp.net then looks for any controls that have been updated throughout the page and performs the RaisePostDataChanged event action. However, this event is not done until all the controls have been updated and the Load event is completed. This ensures that one control will not be manually changed in the RaisePostDataChanged event until it is updated by postback data.
VI. Handle client-side PostBack events
When the events raised by the postback data on the server side are completed, the object that produces the postback data performs the RaisePostBackEvent event operation. However, there is a situation where a control's state changes so that it returns the form to the server or the user clicks the submit button to make the form return to the server. In this case, there should be corresponding processing code to reflect the event-driven object-oriented (OOP) programming principle. In order to meet the accuracy requirements of the data presented to the browser, the RaisePostBackEvent event is the last in a series of postback events.
Controls that change during postback should not be updated after the execution function is called. That is, any data that changes due to an expected event should be reflected on the final page. You can modify the RaisePostBackEvent function to meet your requirements.
Seven. pre-presented object
The last moment when you can change the object and save the change is this step-- pre-render the object. In this way, you can make final changes to the properties of the control, the control tree structure, and so on. At the same time, there is no need to consider any changes made to it by Asp.net, because at this point it has been separated from database calls and viewstate updates. After this step, all changes to the object will finally be determined and cannot be saved to the viewstate of the page. You can overload this step through OnPreRender.
VIII. Save ViewState
The viewstate is saved after all the changes to the page control are completed. The state data of the object is still retained in the hidden control, and the object state data presented to the Html is also obtained from here. In the SaveViewState event, its value can be saved to the viewstate object, but the modification of the control on the page cannot be made at this time. You can use SaveViewState to overload this step
Nine. Present it to Html
The Render event occurs when you use Html to create a page for browser output. During the Render event, the page calls the objects in it to present them to the Html. The page can then be accessed by the user's browser as Html. When the Render event is overloaded, developers 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 the Html on the browser as a web page. Some changes can still be made at this time, but they are just changes on the client side. You can overload the Render event
Ten. Destroy the object
After the presentation to the Html is complete, all objects should be destroyed. In the Dispose event, you should destroy all objects that were created when the page was created. At this point, all the processing is done, so destroying any remaining objects will not cause an error, including the page object. You can overload the Dispose event
Thank you for your reading, the above is the content of "how to understand the life cycle of asp.net pages", after the study of this article, I believe you have a deeper understanding of how to understand the life cycle of asp.net pages, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.