Home > DeveloperSection > Articles > PostBack in ASP.Net

PostBack in ASP.Net


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 13418  View(s)
Rate this:

PostBack in ASP.Net


PostBack is an event that is triggered when an action is performed by an ASP.Net Control.  For example when we click on an asp button, the data on the page is posted back to the server for processing. PostBack is the name given to the process of submitting an ASP.NET page to the server for processing.

IsPostBack

‘IsPostback’ is normally used on page _load event to detect whether page is going to reload(i.e postback or refresh) due to any asp control  of page.

IsPostBack having a boolean value; thus, the first time that the page loads the IsPostBack flag is false. Each time a PostBack occurs, the entire page including the Page_Load is ‘posted back‘and executed.

IsPostBack is used to check that a page is submitted to server first time or second time rendering, if code are writen  in ‘isPostBack’ block,
If( isPostBack())

{
//----------------- your code here-----------------//
}


This code will execute only once when this page runs first time. On any other submit event this page will be submitted to server but this block will not execute.

IsPostBack ----------------1st Time -------------------------------False

IsPostBack----------------2nd Time--------------------------------True

!IsPostBack----------------1st  Time-------------------------------True

!IsPsotBack-----------------2nd Time------------------------------ False

 

_doPostBack

We can call manual  PostBack by using the _doPostBack

For Example

__doPostBack('MyTragetID', 'OtherInformation');

In fact only two ASP.NET web server controls causes  PostBack (Button, ImageButton). The other controls call __doPostBack JavaScript function. Take a look how the server controls render as HTML, you'll figure out that the buttons do not call the __doPostBack JavaScript function:

For example add DropDownList and change AutoPostBack property to True. Now, run the page and view the source of the page, you will see the following output:

  <script type="text/javascript">

//<![CDATA[

var theForm = document.forms['form1'];

if (!theForm) {

    theForm = document.form1;

}

function __doPostBack(eventTarget, eventArgument) {

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

        theForm.__EVENTTARGET.value = eventTarget;

        theForm.__EVENTARGUMENT.value = eventArgument;

        theForm.submit();

    }

}

//]]>

</script>

<div>

       <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKonsDTCwKd5I/lCkCWq8WBVW3lejkFNEQSjVdd801K" />

</div>

    <div>

    </div>

    <select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">

</select>

 

 

Here the DropDownList will call __doPostBack function when you change the DropDownList selection. Also you will find that with CheckBox, CheckBoxList, DropDownList, RadioButtonList, RadioButton, ListBox, TextBox. But when you add ASP.NET Button you will find the following HTML output:

<input type="submit" name="Button1" value="Button" id="Button1" />

 

Note that the __doPostBack function and the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared by adding ScriptManger or Server controls that make PostBack except Buttons and ImageButton.

AutoPostBack

AutoPostBack refers to submitting a POST request to the server automatically when a certain client side event of an HTML form element is triggered. Consider a case where an HTML page consists of a DropDownList (<SELECT> element) and a table. Depending on the item selected in the DropDownList you need to fetch data from SQL server and display it in the table. One way to do this is to select an item from the DropDownList and click on a "Submit" button so as to send the selection made to the server. A more logical arrangement would be to submit the form as soon as the selection in the DropDownList changes. This later mechanism is called "AutoPostBack". An AutoPostBack can be of two types:

1.     Full page AutoPostBack

In the case of a full page AutoPostBack, the entire web page is POSTed to the server when selection in a DropDownList changes. This arrangement is suitable in cases where a large part of the page will be affected due to the change in the selection. The advantage of this approach is that it requires little client side JavaScript code. However, the downside is that the entire page will be refreshed thus requiring more time and bandwidth to re-display the page.

2.     Partial page AutoPostBack

In the latter case, i.e., partial page AutoPostBack, instead of submitting the entire page, an AJAX call is made to the server side code to retrieve dependent data. Upon receiving the data the page is updated with the new data using a client side script. The advantage of this method is that the entire form is not submitted to the server. Instead only the required data is POSTed to the server resulting in much lesser traffic and time. The downside is that a reasonable amount of client side code may be required to fetch the data and update the page.


Don't want to miss updates? Please click the below button!

Follow MindStick