Discover which Control Raised a PostBack

March 1, 2010

5 comments

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.


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

5 comments

  1. KannanMarch 19, 2010 ב 10:25

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

    Reply
  2. Gil FinkMarch 20, 2010 ב 13:02

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

    Reply
  3. bosleMarch 25, 2010 ב 3:29

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

    Reply
  4. phhammevqjm@gmail.comDecember 13, 2013 ב 4:06

    Denn auf unserem Geschenke ferner Geschenkideen Blog präsentieren wir schöne Dinge und Wohnkamine.
    Wir ziehen umher und sehen immer wieder mal was uns
    gefällt und das präsentieren wir hier. Auf jeden fall können Sie die meisten Produkte auch
    sofort kaufen, doch ausschließlich über amazon, damit ihr Einkauf
    sicher ist natürlich!

    Reply