Home > Computer Programming, Problem Solving/Puzzles, Web Programming > How to check which control has caused Post Back !

How to check which control has caused Post Back !

My this post will be how to get the control that caused the Post back in ASP.NET code behind. I got this help from this page, all my courtesies to this guy :).

We will have to access the __EVENTTARGET element of the form. If any one has ever seen a client side HTML source of server side HTML code he/she must be familiar with __EVENTTARGET. A hidden tag is added to the form named __EVENTTARGET after a postback. This hidden input is set to the name of the control that was clicked in the __doPostBack JavaScript function(causing postback) and then the form is submitted. We can access this hidden input from our code-behind as it is submitted with the form and can be found in the Params or Form collections. This is how we get the control that caused a postback. Once we have the name you can get a reference to the control via FindControl and use it as needed.

string ctrlname = page.Request.Params.Get(“__EVENTTARGET”);
if (ctrlname != null && ctrlname != string.Empty)
{
return this.Page.FindControl(ctrlname);
}

The only problem of this method is if post back is caused by a button control. This is because a button is rendered as an input type=”submit” tag, and this causes the form to submit only, so a post back is not occured. Thus button will be added to the form collection of the page. So we can get this very control by a little bit of programming logic. So the code will be some like follows.

public static Control GetPostBackControl(Page page)
{
Control control = null;

string ctrlname = page.Request.Params.Get(“__EVENTTARGET”);
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control;
}

This method takes a parameter which is a reference to the Page, it then uses that to look for the control that caused the postback. You can easily use this as follows:

Control c = PageUtility.GetPostBackControl(this.Page);
if (c != null)
{
//…
}

  1. Columbia
    March 1, 2007 at 4:55 pm

    Nice site try it here.

  2. Rishi Agrawal
    March 15, 2007 at 11:46 am

    Hi,

    Thanks for this stuff. Very good explanation.

    Thanks & Regards,

    Rishi Agrawal

  3. May 25, 2007 at 9:54 am

    Thank You

  4. mads
    May 25, 2007 at 9:59 am

    protected void Page_PreRender(object sender,EventArgs e)
    {
    if (IsPostBack)
    {
    string[] ctlPostBack;
    Control findControl = null;
    string jScript=String.Empty;

    ctlPostBack = this.Request.Params.GetValues(“__EVENTTARGET”);
    if (ctlPostBack != null && ctlPostBack.Length > 0)
    {
    //do nothing..
    }
    else
    {
    foreach (string ctl in this.Request.Form)
    {
    Control c = this.FindControl(ctl);
    if (c is System.Web.UI.WebControls.Button)
    {
    findControl = c;
    break;
    }
    }
    }
    if (findControl != null)
    {
    string ctlClientId;
    ctlClientId = findControl.ClientID;
    if (ctlClientId == “btnSubmit”)
    {
    if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
    {
    jScript = ” document.getElementById(‘” + ctlClientId + “‘).focus(); document.getElementById(‘”
    + ctlClientId + “‘).scrollIntoView(true) alert(‘Hey session expired’); “;
    }
    else
    {
    jScript = ” document.getElementById(‘” + ctlClientId + “‘).focus(); document.getElementById(‘”
    + ctlClientId + “‘).scrollIntoView(true) alert(‘Thanks for submitting’); “;
    }
    }

    this.RegisterStartupScript(“focus”,jScript);

    }
    }
    }

    It is not at all working for an ASP button control..

    ctlPostBack is not null nor empty even for a button but not able to get the button control.. please help me..

  5. August 12, 2007 at 4:09 am

    deposit free bonus casino
    see to signature…

  6. August 18, 2007 at 2:54 am

    popular free ringtones

    http://www.thehotstop.info

    signature…

  7. September 18, 2007 at 6:51 am

    Hi

    Very interesting information! Thanks!

    Bye

  8. June 25, 2008 at 12:30 pm

    what about Request[“__EVENTARGUMENT”]?

  9. Rudra
    September 23, 2008 at 11:54 am

    what about Request[“__EVENTARGUMENT”]?

  10. James
    October 27, 2008 at 4:05 pm

    Good, but i saw the same content which was created in 2005 at http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx too, may be a sheer coincedence 🙂

  11. October 28, 2008 at 6:54 pm

    This is really cool… I can be sensitive about my murky violence I have a good fresh joke for you! How can you tell if a planet is married? It has a ring around it.

  12. March 12, 2010 at 3:22 pm

    If you want your buttons or image button to subnit a value in the “__EVENTTARGET” you can add a JavaScript to do so. I added it in the mouseup event

    ibnBuscar.Attributes.Add(“onmouseup”, “Sys.WebForms.PageRequestManager.getInstance()._doPostBack(\”ibnBuscar\”, \”\”)”);

    the “ibnBuscar” is the ID of my imagebutton

  13. July 3, 2013 at 9:28 am

    Fastidious answers in return of this issue with genuine arguments and explaining the whole thing on the topic of that.

  1. No trackbacks yet.

Leave a reply to casinosfreebonusesv Cancel reply