Gil Fink on .Net

Fink about IT

News

Microsoft MVP

MCPD Enterprise Applications Developer

Gil Fink

My Linkedin profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010 Gil Fink

Blog Roll

Hebrew Articles

Index Pages

My OSS Projects

Discover which Control Raised a PostBack

Discover which Control Raised a PostBack

Yesterday I Discover which Control Raised a PostBack
needed a
solution for
an annoying
problem. I have
some buttons
on a ASP.NET
web form
and I need to know which button raised the postback not in the
event itself but in the page load event. This post will show a 
way to solve this conundrum.

Discover which Control Raised a PostBack

When we use ASP.NET and we have ASP buttons on the page
if we want to do something before their postback event happen
we need to discover whether they raised a postback. Since
an ASP button uses the form.submit() method on the client side
then on the server side the
Page.Request.Params[“__EVENTTARGET”] will return null.
The following method will discover which control raised a postback:

private Control GetControlThatRaisedPostBack()
{           
   string id = Page.Request.Params["__EVENTTARGET"];
   if (!string.IsNullOrEmpty(id))
   {
       return Page.FindControl(id) as Control;
   }
   foreach (var ctlID in Page.Request.Form.AllKeys)
   {
       Control c = Page.FindControl(ctlID) as Control;
       if (c is Button)
       {
           return c;
       }
   }
   return null;
}

As you can see if the control isn’t a button then it will
have __EVENTTARGET which will be handled first. If we
deal with ASP buttons then we need to iterate on the form
keys and through them we will find the button. This is because
when a button is clicked it uses the form.submit() method.
That method add the button to the list of elements that are
submitted in the form (and only the button that called the submit
and no other buttons).

Summary

Finding which control raised a postback in the form outside
the postback event can be very annoying. I hope that the method
I provided will be helpful to you.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# March 1, 2010 9:25 AM

Ricardo Peres said:

You can find a slightly more advanced solution in weblogs.asp.net/.../find-control-that-caused-a-postback.aspx.

# March 1, 2010 12:38 PM

Kannan said:

Good idea. But what will happen if the form has more than one button? Can we identify which one raise the postback?

# March 19, 2010 10:25 AM

Gil Fink said:

@Kannan,

If the form has more than one button this solution will also work. This is happening because the form.submit() method (which is used in case a button was clicked) add the button to the list of elements that are submitted in the form and only that button.

# March 20, 2010 1:02 PM

[Truquillo]Descubre qu?? control dispar?? el Postback « El blog de Quique Fagoaga said:

Pingback from  [Truquillo]Descubre qu?? control dispar?? el Postback « El blog de Quique Fagoaga

# March 20, 2010 10:13 PM

bosle said:

fine its good that we can identify the controls which raised postback

# March 25, 2010 3:29 AM