ASP.NET - Postback in asp.net

Asked By Muralikrishnan on 29-Oct-10 02:53 AM
hi
what is the difference between if(isPostback) and if(!isPostback)
I am getting confused..pls explain me

thx in advance
Sagar P replied to Muralikrishnan on 29-Oct-10 02:57 AM
In ASP.Net, the Page Load is event is fired every time a page is loaded. This is true even if the page is loaded as part of a submit button click. You may need to identify if the page load event is called as part of a page submit or if it is a fresh page rendering. Because, in case of page submit, you may want to skip several task like populating the data again etc.

The IsPostBack property can be used to determine if the page is submitted to itself. When a form is posted (submitted) back to the same page that contains it, it's called a post back. ASP.NET provides a property called IsPostBack that is TRUE when the page is being loaded as a result of a post back, and is FALSE otherwise.

We can check the value of this property and based on the value, we can populate the controls in the page. If the property is TRUE, that means it is a postback (submit) and no need to populate all the values again.

Sub Page_Load
     IF Not IsPostBack THEN
                ' Need to do load the grid only if it is first time load.
                grdtest.DataSource = Session("dS")
                grdtest.DataBind()
      END IF
End Sub
Nowshad M replied to Muralikrishnan on 29-Oct-10 03:00 AM
Hi,
  if (!IsPostBack)
    {

      Response.Write("first time the page is loaded.");

    }
    else
    {
      Response.Write("It comes here when the page is loaded more than once");
    }
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.


Sujit P replied to Muralikrishnan on 29-Oct-10 05:18 AM
Hi

1> If Not Page.isPostback will be true only when the page is loaded for the first time or you request/refresh the page. Here are a few scenarios.
   1.1> Window.open(page.apx),
   1.2>Response.Redirect(Page.aspx)
   1.3>explicitly requesting a page by typing its URL or refreshing as open page.

2> Scenarios where If Not Page.IsPostback is false,
    2.1> When the page is reloaded due to button click,    
    2.2> dropdownlist selectedindexchange  
    2.3> or due to any such serverside (asp.net) controls
    2.4>you use JavaScript (client controls/HTML controls) to do an explicit postback using __doPostBack() function

As a std practise, I would recommend that you always use :-
 
  If Not Page.IsPostBack Then
      ........
  Else
   ......(this is same as "If Page.IsPostBack" )
 End If