The page. ispostback attribute of ASP. NET

Source: Internet
Author: User
Now, starting from the execution sequence of ASP. NET pages, let's take a look at the characteristics of the B/S structure program. The execution sequence of ASP. NET pages is described as follows:

Page_init (events triggered by PAGE initialization) --> page_load (events triggered when a page is loaded) --> Control event (events triggered by a Server Control) --> page_unload (events triggered when the page is detached from the memory)

Page_init and page_unload are not commonly used. The difference between the page_init and page_load events is that only the latter can fully load the control and bind data. Although you can access the control in page_init, its viewstate will not be loaded, therefore, only the default value is available in the control.

The viewstate is mentioned here. Let's take a rough look at it-in fact, there are two viewstates in ASP. NET. One is the control itself, which is used to maintain some states of the control. For example, if a space has a color changing function, its viewstate maintains this function. This viewstate cannot be accessed by users. I believe that all my friends who have written controls will feel this way. Of course, they will also use their own viewstate to maintain the status of the control. The other viewstate is used by users, this viewstate is almost the same as the session, and must be defined before it can be used.

Every time you click a button, linkbutton, or imagebutton control on an ASP. NET web page, the form is sent to the server. If the autopostback attribute of some controls is set to true, after the control's status is changed, the form will also be sent back to the server .? (Autopostback attribute, which has only two bool values, true/false. If this attribute is set to false, the changes will not be immediately transmitted to the server for processing after the click, and the selectedindexchanged event of the control will not exist .)

Each time a form is sent back to the server, it is reloaded, The page_load event is started, and all code in the page_load event processing program is executed (note that it is executed every time !).
Obviously, it is most appropriate to put the webpage initialization code here. We often want to execute some code every time a webpage is loaded, such as data binding of some controls.

When we want to execute some other code only when the webpage is loaded for the first time (basically the default binding of data), we even want some code to be executed at each load except for the first load. We can use the ispostback feature to complete this function. When a webpage is loaded for the first time, the value of this attribute is false. If the webpage is reloaded due to sending back, the value of the ispostback attribute is set to true.

In ASP. NET applications, if you need to perform initialization operations when the page is displayed for the first time, you must determine the ispostback attribute!

In ASP. net use page. ispostback, you can avoid extra work on the round-trip: If you process the Server Control to send back, you usually need to execute the code on the first request page, this code is different from the code used for round-trip when an event is triggered. If check? Page. ispostback? The code can be executed according to the conditions, depending on whether there is an initial request to the page or a response to the server control event. This seems obvious, but you can ignore this check without changing the page behavior. The usage of this attribute is directly related to whether your program runs as expected, and the efficiency of the entire page. Because, if you bind data to the control every time, no matter whether you access the control for the first time or submit the data, the efficiency of the page program can be imagined.

Each time a page with a B/S structure is submitted, it will be executed again from start to end. The C/S structure program won't be like this, which is the biggest difference with the C/S structure program! In fact, the data of the control is not obtained for this reason.

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.