Saturday, January 14, 2012

What is post back in Asp.Net


PostBack is the name given to the process of submitting an ASP.NET page to the server for processing . PostBack is done if certain credentials of the page are to be checked against a database (such as verification of username and password). This is something that a client machine is not able to accomplish and thus these details have to be ‘posted back’ to the server.
A simple example to illustrate the usage of PostBack is a login page. After the user has typed his username and password, he clicks on the ‘Login’ button. Upon the click, the page is sent to the server to check against the database/XML file to check if the user with supplied details is an authenticated user.
Then there arise certain events ( Listbox Index Changed,RadioButton Checked etc..) in an ASP.NET page upon which a PostBack might be needed. Consider the case in which you have 2 ComboBoxes. Let us say the first ComboBox asks you for the Country you reside in and the second one asks you for the State/Province in that country. Based upon the Country you select, the list of States/Provinces must be shown. Thus, in order to fill the values in the second ComboBox, the selection in the first ComboBox must be known to the server. Thus, as and when an item is selected in the first ComboBox, a PostBack must be done and the appropriate list of items must be filled in the second ComboBox.



Processing Page Requests
When an initial request for a page (a Web Form) is received by ASP.NET, it locates and loads the requested Web Form (and if necessary compiles the code). It is important to understand the sequence of events that occurs when a Web Forms page is processed. This knowledge will help you program your Web Forms pages and Web applications more effectively.
As described before, initial page requests are relatively simple. The real work gets done when a page is submitted to itself - and a postback request is generated. Here are a few notes on postback requests:
  • The current value of every control on a Web Form is contained in the postback request. This is referred to as the Post Data
  • The content of the ViewState is also contained in the Post Data. ViewState holds theoriginal property values of every control on a Web Form - before the user made any changes
  • If a postback was caused, for example, by a button click, Post Data is used to identify the button that caused the postback
Postback Event Processing Sequence
Here are the events (and the order) that are raised when a Button is clicked and a postback occurs:
  1. Page.Init + Control.Init for every control on the Web Form
    The first stage in the page life cycle is initialization. After the page's control tree is populated with all the statically declared controls in the .aspx source the Init event is fired. First, the Init event for the Page object occurs, then Init event occurs for each control on the Page. Viewstate information is not available at this stage.
  2. Page.LoadViewState
    After initialization, ASP.NET loads the view state for the page. ViewState contains the state of the controls the last time the page was processed on the server.
  3. Page.ProcessPostData
    Post Data gets read from the request and control values are applied to control initalized in stage 1.
  4. Page.Load + Control.Load for each control on the Page
    If this is the first time the page is being processed (Page.IsPostback property), initial data binding is performed here.
  5. "Change" events are fired for controls (TextChanged, SelectedIndexChanged, and similar)
    The current value (from Post Data) is compared to the original value located in the ViewState. If there is a difference "Changed" events are raised.
  6. Server-side events are fired for any validation controls
  7. Button.Click + Button.Command
    The Click and Command events are fired for the button that caused the postback
  8. Page.PreRender + Control.PreRender
  9. Page.SaveViewState
    New values for all the controls are saved to the view state for another round-trip to the server.
  10. Page.Render

No comments:

Post a Comment

Live

Your Ad Here