Source of a PostBack

Basics of what a PostBack event is:  Most ASP.NET Controls are “Server Controls”. This means that when an action occurs, the Page, which hosts all its controls inside. But, how does the Page class “know” which control was “clicked, changed, or whatever”? Very simply, each control generates plain old client-side Javascript. Since HTTP is stateless and there is no “invisible connection” between the server-side classes and the client-side HTML document in your browser, how can a client-side event be handled on the server side? In ASP.NET, this is done by using hidden form fields. A client-side event, such as an “onclick” or “onchange” event, can be captured by a JavaScript event handler. ASP.Net provides the javascript __doPostBack() function. This records the name of the object that fired the event, as well as any additional event information, places it into the hidden form fields __EVENTTARGET and __EVENTARGUMENT, and then submits the form, initiating our PostBack.
On the server side, any controls that implement either the IPostBackDataHandler or IPostBackEventHandler interfaces (which means that they can process client-side events) will have the appropriate method called to examine the PostBack data and see if they have raised the client-side event. If so, the corresponding server-side event is raised and the event handler method is called. You can create your own custom PostBack by simply calling the ASP.NET “GetPostBackEventReference” method.

 Global.asax.cs codebehind file:

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;

namespace WhichControlPostedBack{
 public class Global : System.Web.HttpApplication{
  private System.ComponentModel.IContainer components = null;

  public Global(){
   InitializeComponent();
  } 

public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page){
  Control control = null;
  string ctrlname = page.Request.Params["__EVENTTARGET"];
  if (ctrlname != null && ctrlname != String.Empty){
   control = page.FindControl(ctrlname);
  }
  // if __EVENTTARGET is null, the control is a button type and we need to 
  // iterate over the form collection to find it
  else{
   string ctrlStr=String.Empty;
   Control c=null;
   foreach (string ctl in page.Request.Form){
    // handle ImageButton controls ...
    if (ctl.EndsWith(".x") || ctl.EndsWith(".y")){
     ctrlStr = ctl.Substring(0,ctl.Length-2);
     c= page.FindControl(ctrlStr);
    }
    else{
     c = page.FindControl(ctl);
    }
          if (c is System.Web.UI.WebControls.Button || 
		c is System.Web.UI.WebControls.ImageButton){
     control = c;
     break;
    }
   }
  }
  return control;
 }
  private void InitializeComponent(){
   this.components = new System.ComponentModel.Container();
  }
 }
}

Its a static method – which means you can reference it without a class instance, simply by calling Global.GetPostBackControl(this), with the “this” being a reference to the Page class that you are in. Typically we would do this as follows, in the Page_Load event handler method:

private void Page_Load(object sender, System.EventArgs e){
if(IsPostBack)
Label1.Text=Global.GetPostBackControl(this).ID.ToString();
} 

source: ASP.NET: Which Control Posted Back?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s