What is PostBack in ASP.NET

Introduction

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 a 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.

What is AutoPostBack Property in ASP.NET

If we create a web page that consists of one or more Web Controls that are configured to use AutoPostBack (Every Web control will have its own AutoPostBack property), ASP.Net adds a special JavaScipt function to the rendered HTML Page. This function is named _doPostBack(). When Called, it triggers a PostBack, sending data back to the web Server.

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

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />  
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />  

The _doPostBack() function has the responsibility for setting these values with the appropriate information about the event and 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.

Any Control that has its AutoPostBack Property set to true is connected to the _doPostBack() function using the onclick or onchange attributes. These attributes indicate what action the Browser should take in response to the Client-Side javascript events onclick and onchange.

In other words, ASP.NET automatically converts a client-side Javascript event into a server-side ASP.NET event, using the _doPostBack() function as an intermediary.

Life Cycle of a Web Page

To work with the ASP.Net Web Controls events, we need a solid understanding of the web page life cycle. The following actions will take place when a user changes a control that has the AutoPostBack property set to true.

  1. On the client side, the JavaScript _doPostBack function is invoked, and the page is resubmitted to the server.
  2. ASP.NET re-creates the Page object using the .aspx file.
  3. ASP.NET retrieves state information from the hidden view state field and updates the controls accordingly.
  4. The Page. Load event is fired.
  5. The appropriate change event is fired for the control. (If more than one control has been changed, the order of change events is undetermined.)
  6. The Page.PreRender event fires and the page is rendered (transformed from a set of objects to an HTML page).
  7. Finally, the Page. The unloading event is fired.
  8. The new page is sent to the client.

To watch these events in action, we can create a simple event tracker application. All this application does is write a new entry to a list control every time one of the events it's monitoring occurs. This allows you to see the order in which events are triggered.

event tracker in ASP.NET 

Event Tracker Web Page

I have shown Markup codes and C# Codes below to make this work.

EventTracker.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EventTracker.aspx.cs" Inherits="EventTracker" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <p>
            <h1>Controls being monitored for change events:</h1>
            <asp:TextBox ID="txt" runat="server" AutoPostBack="true" OnTextChanged="CtrlChanged" />
            <br /><br />
            <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="CtrlChanged" />
            <br /><br />
            <asp:RadioButton ID="opt1" runat="server" GroupName="Sample" AutoPostBack="true" OnCheckedChanged="CtrlChanged" />
            <asp:RadioButton ID="opt2" runat="server" GroupName="Sample" AutoPostBack="true" OnCheckedChanged="CtrlChanged" />
            <h1>List of events:</h1>
            <asp:ListBox ID="lstEvents" runat="server" Width="355px" Height="150px" /><br />
            <br /><br /><br />
        </p>
    </form>
</body>
</html>

 EventTrakcer.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class EventTracker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Log("<< Page_Load >>");
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        // When the Page.PreRender event occurs, it is too late to change the list.
        Log("Page_PreRender");
    }

    protected void CtrlChanged(Object sender, EventArgs e)
    {
        // Find the control ID of the sender.
        // This requires converting the Object type into a Control class.
        string ctrlName = ((Control)sender).ID;
        Log(ctrlName + " Changed");
    }

    private void Log(string entry)
    {
        lstEvents.Items.Add(entry);
        // Select the last item to scroll the list so the most recent
        // entries are visible.
        lstEvents.SelectedIndex = lstEvents.Items.Count - 1;
    }
}

What the above Code does

The code writes to the ListBox using a private Log() method. The Log() method adds the text and automatically scrolls to the bottom of the list each time a new entry is added, thereby ensuring that the most recent entries remain visible.

All the change events are handled by the same method, CtrlChanged(). If you look carefully at the .aspx file, you'll notice that each input control connects its monitored event to the CtrlChanged() method. The event handling code in the CtrlChanged() method uses the source parameter to find out what control sent the event, and it incorporates that information in the log string.

The page includes event handlers for the Page. Load and Page.PreRender events. As with all page events, these event handlers are connected by method name. To add the event handler for the Page.PreRender event, you simply need to add a method named Page_PreRender(), like the one shown here.

Thank You...


Similar Articles