Thursday, 11 August 2011

Post Back in ASP .Net

There are some definition of postback.
Post back is the process the sending data get back to server
for processing.
Is post back property checks whether page is being loaded for
first time.
if it is False :means first time loaded.
if it is True: means round trip to server.
=============
Postback: Postback is a event which fire when page data goes to server.
IsPostBack: Ispostback is page property of bool type and it become true when page come again to browser.
===
PostBack: Postback is the event which sends the form data
to the server. The server processes the data & sends it
back to the browser. The page goes through its full life
cycle & is rendered on the browser. It can be triggered by
using the server controls.And the IsPostBack property of
the page object may be used to check whether the page
request is a postback or not. IsPostBack property is of the
type Boolean.
=======
Today we are going to introduce you to the concept of PostBack 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.

To handle these situations, you need to enable the ‘Auto PostBack’ property for those controls whose events are going to trigger some kind of server processing (the case of the first ComboBox in the previous example).

Usage of IsPostBack in ASP.NWT-

IsPostBack is a Boolean property of a page when is set (=true) when a page is first loaded. Thus, the first time that the page loads the IsPostBack flag is false and for subsequent PostBacks, it is true. An important point to be noted here is that each time a PostBack occurs, the entire page including the Page_Load is ‘posted back‘ and executed.

Thus a very common programming practice is that whenever some part of the code is to be executed just the first time a page loads, it is checked against the IsPostBack flag.

If you have not understood the concept clearly, do not worry. Programming examples in subsequent posts will help making this concept clear.
=====
Page.IsPostBack is for forms that are runat=”server”. It is mostly used for same page validation, same page operations, … anything really same page!

It is also used to help keep bandwidth and rendering times down.

The main reason why people use IsPostBack is to stop taping the databases everytime a request is made. If you are on your default page and just do work via that page, you can use the if Not page.ispostback then statements to populate information. When pages are posted back to the sever and then rendered back to the client, they don’t have the need to repopulate the information since it was just populated. It is all kept in the viewstate and read back without using precious resources on the server.

It’s a very good and handy tool to know, and very easy to know as well! Any information that is not variable dependant should be only posted when the user first shows the page.

For another example, let’s say you are using menus that pull information from the database, articles that are previewed on a side bar for users with a link to the full article, and a search function that posts results that can be ordered by date, name, etc. Now, with the IsPostBack functionality, you can add the statement to all information that does not change when a request is made on the same page. You can add the “If Not Page.IsPostBack Then” for the menus and articles since the information does not change in anyway when a user clicked to have the search results ordered by name! This means that you do not have to tap the database three times, but only once! The information about the menus and articles are held in the viewstate so you don’t have to repopulate them.

Then for the “If Page.IsPostBack Then” statement, you can use for registration purposes for your site, post comments, etc. all by the Page.IsPostBack function. No code in the “If Page.IsPostBack Then” statement is fired when the page is requested by the client for the first time; And vice-versa, the “If Not Page.IsPostBack Then” function is only fired when the page is called for the first time, then uses viewstate to fill in the areas it is required to for every postback after that.

Hope this helps..

********************************************************************

Page.IsPostBack Is used to check if the user posted back to the page. Meaning they clicked a button or changed a field in the form that causes it to post back to itself.

Putting Not usually is used to check the initial load of the page.

You only need to fill databound fields on the initial load, so you put all your databinding code in the Not page.IsPostBack. The values are saved in the viewstate and can only be read by the form if it gets posted back to itself.

So, if it is a postback dont do databinding if its not a postback do databinding.


5 comments:

  1. very good information keep going

    ReplyDelete
  2. The article is very helpful..

    ReplyDelete
  3. IsPostBack is a property of the Asp.Net page that tells whether or not the page is on its initial load or if a user has perform a button on your web page that has caused the page to post back to itself.

    http://net-informations.com/faq/asp/ispostback.htm ispostback() in asp.net

    ling

    ReplyDelete
  4. good explanations and helpfull thanx

    ReplyDelete