Click here to Skip to main content
15,881,709 members
Articles / Web Development / ASP.NET

Understanding The Complete Story of Postback in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (11 votes)
26 Aug 2014CPOL4 min read 200.7K   11   1
Understand the Complete Scene of Postback in Asp.net with Examples

Introduction

In the old HTML, the only way to make something updated on the webpage is to resend a new webpage to the client browser. That's what ASP used to do, you have to do this thing call a "PostBack" to send an updated page to the client.

In ASP .NET, you don't have to resend the entire webpage. You can now use AJAX, or other ASP.NET controls such that you don't have to resend the entire webpage.

If you visit some old website, you would notice that once you click something, the entire page has to be refresh, this is the old ASP. In most of the modern website, you will notice your browser doesn't have to refresh the entire page, it only updates the part of the content that needs to be updated. For example, in Stackoverflow, you see the page update only the content, not the entire webpage.

Programming model in old ASP for using POST method in form is to post the values of a Form to a second page. The second asp page will receive the data and process it for doing any validation or processing on the server side.

With ASP .Net, the whole model has changed. Each of the asp .net pages will be a separate entity with ability to process its own posted data. That is, the values of the Form are posted to the same page and the very same page can process the data. This model is called post back.

Each Asp .net page when loaded goes through a regular creation and destruction cycle like Initialization, Page load etc., in the beginning and unload while closing it. This Postback is a read only property with each Asp .Net Page (System.Web.UI.Page) class. This is false when the first time the page is loaded and is true when the page is submitted and processed. This enables users to write the code depending on if the PostBack is true or false (with the use of the function Page.IsPostBack()).

Understanding the PostBack

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 some sources (such as verification of username and password using database). 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 post back is round trip from the client (Browser) to the server and then back to the client. This enables you page to go through the asp engine on the server and any dynamic content to be updated.

Using the Code

Post back is implemented with the use javascript in the client side. The HTML page generated for each .aspx page will have the action property of the form tag set to the same page. This makes the page to be posted on to itself. If we check the entry on the HTML file, it will look something like this.

<form name="_ctl1″ method="post" action="pagename.aspx?getparameter1=134″ language="javascript" onsubmit="if (!ValidatorOnSubmit()) return false;" id="_ctl1″ >

Also, all the validation code that is written (Required Field Validation, Regular Expression validation etc.,) will all be processed at the client side using the .js(javascript) file present in the webserver_wwwroot/aspnet_client folder.

With this new ASP .Net model, even if the user wants to post the data to a different .aspx page, the web server will check for the runat=’server’ tag in the form tag and post the web form to the same .aspx page. A simple declaration as in the following code snippet will be enough to create such a web form.

<form id="form1″ runat="server" > 
<!– place the controls inside –>
</form>

Understanding the AutoPostBack

AutopostBack is a property which you assign to web controls if you want to post back the page when any event occurs at them.

Using the Code

<asp:DropDownList id="id" runat="server" AutoPostBack="true" OnSelectIndexChanged="..."/>

This ddl not need asp:button for example in order to post, when you change ddl is autoposted.

Defaut value on control is false.

ASP.NET also adds two additional hidden input fields that are used to pass information back to the server. This information consists of ID of the Control that raised the event and any additional information if needed. These fields will empty initially as shown below,

The _doPostBack() function has the responsibility for setting these values with the appropriate information about the event and the submitting the form. The _doPostBack() function is shown below:

<script language="text/javascript">
 
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();

    }
</script>

ASP.NET generates the _doPostBack() function automatically, provided at least one control on the page uses automatic postbacks.

Understanding the Page.IsPostBack

  • Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.
  • The IsPostBack property can be used to determine if the page is submitted to itself. When a form is 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.

Using the code

private void Page_Load()

{

    if (!IsPostBack)

    {

        //You can write here the code, which you want to execute in the first time when the page is loaded.

        FunctionToBindSomething();

    }

}

Usage

  1. The One of the Most Common Use of AutoPostBack is for Cascading Dropdown list.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Cogilent Solutions
Pakistan Pakistan
Muhammad Taqi Hassan Bukhari is a Web Developer by profession, passionate towards open source software, education, and the intersection of both. He loves to read and explore anything open source. He strongly believes that technology and the information about the technology should be available to everybody in their own language.
He enjoy working with his hands, whether it is coding a new website, painting a new painting or even doing yard work. He hope to have a software company one day.

Comments and Discussions

 
Questionasp.net Pin
Member 1225514430-Mar-16 22:38
Member 1225514430-Mar-16 22:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.