RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  Determining the Control that Caused a PostBack 


qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbff080100000000e000000001001800

Many times you might need to perform some action on an ASP.NET postback based on the control that caused the postback to occur. Some scenarios for this might include a form with many regions, each having it's own CustomValidator and the ability to perform a postback when a button for the section is clicked. Another scenario might be to set focus back to the control that caused the postback.

There are two parts to the process of determining which control caused the postback. First, you access the __EVENTTARGET element of the form. If you've ever looked a the anatomy of an ASP.NET page (and if you haven't why are you reading this?), you'll notice that a hidden input tag is added to the form named __EVENTTARGET. This hidden input is set to the name of the control that was clicked in the __doPostBack JavaScript function and then the form is submitted. Looking at how the server controls are rendered as HTML tags you can see that the __doPostBack function is called by the controls to cause a postback, passing the name of the control to the function. You can access this hidden input from your code-behind as it is submitted with the form and can be found in the Params or Form collections. This the first part to getting the control that caused a postback. Once you 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);
}

This will work, but you'll soon find that something is missing. Although this will work for CheckBoxes, DropDownLists, LinkButtons, etc, this does not work for Button controls. This is where the second part comes in. But why is it that this doesn't work for Buttons? If you again take a look at how the server controls render as HTML, you'll see that the buttons do not call the __doPostBack Javascript function so the __EVENTTARGET is never set. Instead, the Buttons render as simple input type=“submit” tags. All the button does is cause the form to submit. That's it. However, you can still get to it, just in a different way. Since the button (or input) is what causes the form to submit, it is added to the items in the Form collection, along with all the other values from the submitted form. It is important to note, that other input type=“submit” tags on the form are not added to the Form collection unless it was the one that caused the form to submit. If you were to look in the Form collection for anything that is a button then that will be what caused the postback (assuming that it was a button that caused the page to submit). If you first check the __EVENTTARGET, then if that is blank look for a button in the Form collection then you will find what caused the postback. The complete code 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)
{
    //...
}

You can now do some specific action based on the control that caused the postback or use an earlier post I made to set focus to the control.




                   



Leave a comment below.

Comments

  1. Alexander 3/22/2005 12:52 AM
    Gravatar
    That article helped me to solve a problem.

    Thank you.
    Alexander
  2. isas 3/28/2005 2:46 PM
    Gravatar
    I have a imagebutton and this doesn't work for that.
  3. Ben Parker 4/5/2005 6:11 PM
    Gravatar
    Legend. Thanks
  4. Rick 4/12/2005 10:35 AM
    Gravatar
    " I have a imagebutton and this doesn't work for that. "

    We re-wrote this in VB and had to deal with imagebuttons too. I will insert the code in VB, and you can convert it to C#, because i'm hungy, and its lunch time, and I don't feel like taking the time to do it myself.

    foreach (string ctl in page.Request.Form)
    {

    ''''''''''''''''''''Begin VB Code''''''''''''''''''''''''''''
    if ctl.EndsWith(".x") or ctl.EndsWith(".y") then
    ctl = ctl.substring(0,ctl.length-2)
    end if
    ''''''''''''''''''''End VB Code''''''''''''''''''''''''''''


    Control c = page.FindControl(ctl);
  5. Rick 4/12/2005 10:36 AM
    Gravatar
    Also note, this will not work if you (for some unknown reason) name your control "MyControl.x" or "MyControl.y"

    Im sure you knew that though
  6. opello 4/19/2005 1:40 PM
    Gravatar
    Also note that this doesn't work for arrays of controls that are created dynamically (or controls that are created dynamically for that matter).
  7. Harnek 4/20/2005 2:28 PM
    Gravatar
    Its Great Thanks
  8. Amol K 5/1/2005 10:38 PM
    Gravatar
    Excellent
  9. crg 5/10/2005 12:09 PM
    Gravatar
    It also doesn't work if you have multiple buttons on your page. It's useless.
  10. toji a mathew 5/14/2005 3:31 AM
    Gravatar
    how to get a selected index of a dropdownlist added in a placeholder control
  11. arkus 5/18/2005 5:16 AM
    Gravatar
    this is not usefully when there are multiple buttons
  12. Imran 5/23/2005 9:58 PM
    Gravatar
    THANKS A LOT FOR A NICE PIECE OF WORK
  13. jairo 7/3/2005 5:08 PM
    Gravatar
    Very useful pal, thanx a lot
  14. David Peck 7/18/2005 6:26 AM
    Gravatar
    Serious love from all over this post, it would seem
  15. GPF 7/20/2005 4:17 AM
    Gravatar
    If you have dynamically added controls then add these controls on page init event. Otherwise you may even loose your events assigned to these controls.
  16. Captain's Blog 9/2/2005 4:55 PM
    Gravatar
  17. Jay Parker 10/11/2005 6:21 AM
    Gravatar
    I found this very helpful. It solved my problem. Thank you!
  18. dhillman 10/18/2005 11:04 AM
    Gravatar
    RadioButtonList controls also pose some problems. The value returned by

    page.Request.Params.Get("__EVENTTARGET")

    is not really the name of the control. By examining the value returned, I am able to extract the real control name, and then pass that to the page.FindControl method.

    Is there a better way to get the name of the real control?

    And the value returned by page.Request.Params.Get("__EVENTTARGET")
    seems to be constructed differently depending upon the control type. Are there any rules to how this value is constructed?

    thanks for the assistance
  19. Surendra 12/8/2005 1:00 AM
    Gravatar
    This really a good article but how do u differentiate with refresh.
    what i mean to say if u refresh teh page it show the name of that control that caiused teh post back earlier.can we avoid that?
  20. Bob 12/8/2005 12:02 PM
    Gravatar
    Nice but it only returns the first button of the page...
  21. Dave 1/31/2006 8:20 PM
    Gravatar
    This is great....It is NOT Useless....the very FIRST button you encounter in the for each loop IS the button that caused the postback even if you have a buttload of buttons on the page.

  22. Ryan Farley 2/1/2006 6:10 AM
    Gravatar
    Thanks Dave,

    It's always worked for me too ;-) (which is why I posted it in the first place!)

    -Ryan
  23. Mayur Joshi 2/22/2006 3:42 PM
    Gravatar
    I am still confuse what will happened if there is more than one button control on the web form. How ‘else’ part of GetPostBackControl method will work?

    Mayur
  24. wombat 3/3/2006 12:49 PM
    Gravatar
    Mayur,

    I think, the way html post works is that only the button that causes the form to submit is posted back.

    On another front, i've seen other implementation, whick involves extending the stock standard button, and attached a piece of javascript to it to set the EVENTTARGET hidden field.

    wombat
  25. Abhrajit 4/6/2006 12:58 AM
    Gravatar
    How do I check for a Datagrid edit column edit link that caused the post back?
  26. Anthony Walsh 4/13/2006 7:38 PM
    Gravatar
    Thank you, thank you, thank you... This is exactly what I was looking for!
  27. Dave Bacher 4/20/2006 6:31 AM
    Gravatar
    Surendra,

    When you hit refresh, ASP.NET will receive the same information that it received the first time. In order to differentiate a refresh from the most recent postback, add a GUID to the viewstate.

    Then whenever you successfully process a postback event, you write the GUID from the viewstate to session. At the start of the routine that processes the postback event, you check to see if the GUID in viewstate matches the GUID in session. If the two match, then it was a refresh browser side. If the two are different, then it is a fresh postback.

    You can use a counter, prng, etc. to generate the numbers -- GUID is just excessively convenient for this purpose. :-)
  28. Geoff Martin 4/26/2006 11:58 AM
    Gravatar
    I have solved A BIG problem with this!!!! thanks a lot, you're my hero!!!!!
  29. Bubba 5/2/2006 3:52 AM
    Gravatar
    You rock dude! :) Thanks!
  30. GT 5/31/2006 11:36 AM
    Gravatar
    I use the "sender" object parameter in the PostBack Event method.

    void ButtonClick(object sender, System.EventArgs e)
    {
    Control ctrl = (Control)sender;
    }

    Of course if you can get the type of the control and cast it to get the controls properties as well.
  31. Dahlsim 8/10/2006 9:35 AM
    Gravatar
    Which is fine if you don't need to know the triggering control until you are in the event handler but what if you need to know earlier such as in Page_Init?
  32. Dave 9/6/2006 2:25 PM
    Gravatar
    Can someone PLEASE post this entire Function in VB? Thanks in advance.
  33. Johny Cash 9/17/2006 1:26 PM
    Gravatar
    C# version which works with ImageButtons as well

    private Control getClickedButton()
    {
    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 = null;

    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
    {
    c = Page.FindControl(ctl.Substring(0, ctl.Length - 2));
    }
    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;
    }
  34. Sandeep 10/9/2006 10:58 PM
    Gravatar
    Hi,

    Can i on Page_Load() determine which control caused the PostBack? Not only just the type of Control, but the Value of the control.

    Thanks in advance.
  35. Ryan Farley 10/10/2006 6:29 AM
    Gravatar
    Sandeep,

    That is exactly what this article describes.

    -Ryan
  36. K 11/7/2006 10:02 PM
    Gravatar
    Nice!!! Been Googling on this one for a while.....
  37. Another programmer 11/9/2006 6:55 AM
    Gravatar
    Thanks and God Bless You.
  38. Paul Reid 12/13/2006 2:55 PM
    Gravatar
    Or, more efficiently,

    if (Request.Form[Button1.UniqueId] == Button1.Text)
    // button1 pressed
    else if (Request.Form[Button2.UniqueId] == Button2.Text)
    // button2 pressed
    else ...

    Unless you have dozens of buttons, this is efficient and clean.

    This avoids the foreach (a hashtable of the Request objects has already been built) and especially the multiple FindControls which are rather inefficient (and occasionally flaky in my experience).
  39. Abhishek 12/14/2006 2:15 AM
    Gravatar
    Hi,
    This article is good. But in my case i have 4-5 buttons in a ascx( user control) file. I click on any of those button post back is caued.
    When i try to identify that which button actaully caused the postback using that "__ EventTarget" i get the name of that ascx file.

    But i am not able to find out which button actaully caused a postback.

    Please help me out with this..... thanks in advance
  40. Pericles Rocha 1/8/2007 11:59 AM
    Gravatar
    I have two buttons on my form and "GetPostBackControl" works great with me... great work, this is VERY usefull, thanks very much...
  41. upalsen 1/18/2007 12:31 AM
    Gravatar
    thanks a lot. I was just looking for this...
  42. Chad Baker 1/20/2007 10:30 AM
    Gravatar
    Ryan, you ARE the man. When I try to find code on the web, the people posting solutions are either freaky brilliant OR concise and to the point, but rarely both. Big ups, thanks!
  43. Ronnie 1/27/2007 1:24 PM
    Gravatar
    I tried to use this code to set focus on a User Control created in a row of datagrid. I got the control id which causes post back. But then it could not load the page as it could not find the control during rendering the page. Any suggestions!!!
  44. Mark 3/27/2007 1:48 AM
    Gravatar
    Hi,

    Great Article, I was wondering if there is an approach where I can find the button which has caused the event in the Application_PreRequestHandlerExecute handler in the Global.asax

    My aim is to create a generic tracer where I track a users last 10 actions in the aim of when there is an application error we can keep a track of what the user has done up until that point.

    I am using .NET 1.1.

    Any help would be great
  45. Chris 4/11/2007 8:47 AM
    Gravatar
    Thanks for the code - this works great if you have a textbox with autopostback=true.

    However, if I have a textbox with autopostback=false, and hit enter, it still posts back if it's the only textbox on the page. In this case, there doesn't seem to be a way to determine the cause of the postback.

    Any thoughts?
  46. Travis J. May 5/30/2007 9:34 AM
    Gravatar
    Thanks a million! This is soooooo basic, I was wondering why the heck it was so hard to do.....
  47. Waleed 7/15/2007 6:30 AM
    Gravatar
    This is exactly what I was looking for .. thanks a lot
  48. Dave 7/19/2007 9:52 AM
    Gravatar
    Thanks! You saved my last piece of hair!
  49. Francisco 8/2/2007 2:42 PM
    Gravatar
    Thanks!
  50. Jonny Upstart 8/14/2007 7:07 AM
    Gravatar
    Nice. I've found this elsewhere, but what I have not found elsewhere is what Paul Reid posted above:

    (if (Request.Form[Button1.UniqueId] == Button1.Text)

    That is much nicer than running through a control collection, especially since I've noticed some peculiar behaviour doing that with a formview.

    Anyway, the necessity of all this at least in my case is that I am trapping a button or linkbutton's event in the Page_Init to then create dynamic controls. If I wait for that button's normal event, it occurs after Page_Load, which is too late for the wiring in the dynamic controls to work. Big pain and not elegant, but hey...
  51. Milan Rawat 8/24/2007 9:40 PM
    Gravatar
    Great Post Bussy.. simply awesome.............
  52. Thai Nguyen 8/30/2007 4:06 PM
    Gravatar
    Thank you, Ryan, for this code - works great!

    Does anyone know how to convert this line of code

    "c is System.Web.UI.WebControls.Button" to Visual Basic?


    Below is what I can convert to Visual Basic
    ---- VB----
    Public Function GetPostBackControl(ByVal myPage As Page) As Control
    Dim ctl As String
    Dim myControl As Control

    Dim ctrlname As String = Page.Request.Params.Get("__EVENTTARGET")
    If (ctrlname <> String.Empty) Then
    myControl = Page.FindControl(ctrlname)
    Else
    For Each ctl In Page.Request.Form
    Dim c As Control = Page.FindControl(ctl)
    myControl = c
    Next
    End If

    Return myControl
    End Function

    --- End VB -----
  53. JimboM 9/18/2007 4:01 PM
    Gravatar
    Translating "c is System.Web.UI.WebControls.Button" to Visual Basic? Simple:

    If TypeOf c Is System.Web.UI.WebControls.Button Then
    ---
    End If
  54. Wole 11/6/2007 6:31 AM
    Gravatar
    Hi,

    This code was exactly what I was looking for. Picture the scenario: I check (from a database) if a user's session has expired in the Page Load event. If their session has expired, I display an appropriate message and redirect to a Login page.

    However, if the user clicks the Logout button and their session has expired in the meantime, ..., you guessed it, they're told they can't logout because they have been logged out. duh!

    Problem--Logout button click event doesn't run until after the Page Load event has run.
    Solution--Use this code int Page Load event to check if the Logout button was clicked, then regardless of whether their session has expired or not, just silently log them out.

    Thanks to Ryan and Paul Reid's code, I was actually able to cut it all down to one line of code (this will work if you know the name of your button and it's a Button or ImageButton [may also work for other controls as well; I don't know...haven't tested other controls]):

    bool LogoutButtonPressed = (Page.Request.Form["btnLogout"] != null || Page.Request.Form["btnLogout.x"] != null);
  55. Marcus Götling 11/14/2007 8:32 AM
    Gravatar
    Awesome!
  56. pakornss 11/27/2007 11:48 PM
    Gravatar
    For ASP.NET 2.0: (with AJAX Extension 2.0)

    ++++++++++++++++++++++++++++++++++++++++++++++

    public static Control GetPostBackControl()
    {
    Page page = (HttpContext.Current.Handler as Page);
    string ctrlname = ScriptManager.GetCurrent(page).AsyncPostBackSourceElementID;

    Control control = page.FindControl(ctrlname);
    return control;
    }

    ++++++++++++++++++++++++++++++++++++++++++++++
  57. Sarah 12/3/2007 3:15 PM
    Gravatar
    I'm trying to figure out this exact same thing in javascript on the client-side.(I have already used this technique server-side and it works.) But I need my client-side validators to also be able to tell which button was clicked (between two buttons) also. I can't figure out how to access the same data structures on the client side. Can you provide any insight?

    Thanks,
    Sarah
  58. Alex 1/24/2008 9:35 PM
    Gravatar
    Thank you!!!
  59. Steve 4/30/2008 3:07 PM
    Gravatar
    Thanks for this, perfect code!
  60. Anchal Jain 5/1/2008 12:30 AM
    Gravatar
    Thanks, was looking for this only !!! Great code
  61. Qaiser Mehmood Mughal 6/11/2008 5:35 AM
    Gravatar
    Thank you.
    Its very helping.........
    nice....

    Regards
    ----------
    QMM
  62. Arun T 8/12/2008 10:26 AM
    Gravatar
    Excellent article! Definitely a life saver in my case. Thanks Ryan!
  63. László Siroki 8/26/2008 8:33 AM
    Gravatar
    In ASP.NET 2.0, you can set the behavior of the button by the UsesubmitBehavior property. If it is set to false, the __doPostBack will be used by the button.
  64. sreeni 8/29/2008 4:26 PM
    Gravatar
    I have Buttomn in UserControl ,This code is not recognizing those buttons ,Help me out.

    Thank you
  65. chirdeep 9/10/2008 4:53 AM
    Gravatar
    Great, I helped me a lot and is exactly what I wanted.
  66. NIRMAL mehta 9/25/2008 12:21 PM
    Gravatar
    Your are Damn Genuis as you solved my problem
  67. Daniel Herrera D. 10/6/2008 4:13 PM
    Gravatar
    When i do postback with a Button, only this control goes in the request so the "foreach" finds it without any problem.

    I used the .ID field to identify my specific button.

    Thanks a lot for the article.
  68. nushahi 11/3/2008 1:29 AM
    Gravatar
    we i put linkbutton in template field in grid view the linkbutton not go to event click why ??
  69. Savas Karaduman 11/25/2008 12:45 PM
    Gravatar
    Very nice article. I solved my problem.
  70. George 2/25/2009 4:57 PM
    Gravatar
    very helpful information. you should start writing books
  71. Joe 2/28/2009 12:18 PM
    Gravatar
    Works great, clean code
  72. Ludvig 3/19/2009 1:59 AM
    Gravatar
    Great article, it solved my problem!
  73. Latha 5/8/2009 5:36 AM
    Gravatar
    Nice article , It solved my problem.

    Thank You Very Much.
  74. slade73 6/9/2009 9:35 AM
    Gravatar
    Thanks!
  75. Amar 6/24/2009 1:05 PM
    Gravatar
    Was Useful.
    Thanks.
  76. Martin 7/1/2009 5:22 AM
    Gravatar
    Brilliant! Absolutely Brilliant!
  77. Karthick 8/4/2009 7:46 PM
    Gravatar
    Fantastic !! It does works with multiple button controls.

    Thanks Man
  78. dangal 9/11/2009 4:15 AM
    Gravatar
    thank you so much
    you helped me alot
  79. Venemo 10/10/2009 9:13 AM
    Gravatar
    This piece of code has just saved me from a half-day-long headache.

    Thank you very much!
  80. Tonique Merrell 10/14/2009 9:03 PM
    Gravatar
    Hey Thanks alot....This really helped me!!
    :)
  81. KCRoyals 10/22/2009 5:58 AM
    Gravatar
    Nice work. Thanks!
  82. Will 10/27/2009 8:52 AM
    Gravatar
    This is terrific!

    It just helped me solve a problem that's been bugging me for a while now. This script has allowed me to intercept instances of a postback being caused by a button (as promised) which were previously being overridden by a dropdown list postback/script that I needed on my page to load components specific to that dropdown selection (and dependent on a dropdown "keyword" being passed to the URL). My buttons weren't working because the postback would always trigger my other dropdown script, but now with *this* script I was able to make that other script conditional, so that if the postback were caused by a button, then the dropdown postback script wouldn't be run.

    Sounds convoluted, but it works great. Thanks Ryan!
  83. Bharat 11/10/2009 5:53 AM
    Gravatar
    It help to solve the problem..thanks
  84. KUDO 11/22/2009 11:12 AM
    Gravatar
    Your method make me passed my problem.

    Very very very thank you.

    Good Luck.

  85. Michael Mournier 11/27/2009 5:15 AM
    Gravatar
    Nice one. I needed to get postback control for a user control and derived a function from code above.

    Here it is!

    ''''''''''''''''''''''''''''''''''''''
    Public Function GetUserControlPostBackControl(ByVal uc As UserControl) As Control

    Dim control As Control = Nothing
    Dim pg As Page = uc.Page
    Dim ctrlname As String = pg.Request.Params("__EVENTTARGET")
    If (Not ctrlname Is Nothing And ctrlname <> String.Empty) Then
    ctrlname = ctrlname.Replace(uc.ID & ":", "")
    control = uc.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

    Dim ctrlStr As String = String.Empty
    Dim c As Control = Nothing
    For Each ctl As String In pg.Request.Form
    'handle ImageButton controls ...
    If (ctl.EndsWith(".x") Or ctl.EndsWith(".y")) Then
    ctrlStr = ctl.Substring(0, ctl.Length - 2)
    ctrlStr = ctrlStr.Replace(uc.ID & ":", "")
    c = uc.FindControl(ctrlStr)
    Else
    ctl = ctl.Replace(uc.ID & ":", "")
    c = uc.FindControl(ctl)
    End If
    If TypeOf c Is System.Web.UI.WebControls.Button Or TypeOf c Is System.Web.UI.WebControls.ImageButton Then
    control = c
    Exit For
    End If
    Next
    End If

    Return control


    End Function
    '''''''''''''''''''''''''''
  86. Dhaval 12/1/2009 12:32 AM
    Gravatar
    hey nice article...i wanted to know were to specific this function as if in page_load() or page_prerender()...
  87. Gratefull Coder 12/1/2009 8:33 PM
    Gravatar
    Its now the December 2nd 2009, and your post is still VALUABLE to the new generation of Programmers in Asp.net thanks thanks THANKZ :-)
  88. Adam 12/6/2009 4:26 PM
    Gravatar
    FYI, I like to use

    if (!string.IsNullOrEmpty(ctrlname))

    instead of

    if (ctrlname != null && ctrlname != string.Empty)
  89. Ryan Farley 12/6/2009 5:36 PM
    Gravatar
    Hi Adam,

    Yes, I use IsNullOrEmpty as well. This article was written before IsNullOrEmpty existed since that was introduced in .NET 2.0.

    Thanks,
    -Ryan
  90. Veerendranath.Darsi 12/8/2009 12:13 AM
    Gravatar
    Normally we can get the name of the control which causes the page post back by the following

    this.Page.Request.Form["__EVENTTARGET"]

    But this will not be applicable for controls like Asp Button and Image Button

    For button or Image button

    If (this.Page.Request.Form.Get(ButtonId.ClientId) != null)

    We can able to know the post back caused by that button.

    (Or)

    this.Page.Request.Form is a string collection we can loop through the collection
    and check whether button control id exist in the list or not.

    Regards
    Veerendranath.Darsi
  91. Troy 12/9/2009 11:57 AM
    Gravatar
    I've used this code for a few years now. I recently updated the code to use LINQ. Here's the result:

    /// <summary>
    /// Gets the control which caused the postback.
    /// </summary>
    /// <param name="page">The page in which the postback occurs. Most like will be "this.Page".</param>
    /// <returns>The control which caused the postback and null if the control was not found.</returns>
    public static Control GetPostBackControl(Page page)
    {
    string eventTarget = page.Request.Params["__EVENTTARGET"];
    if (!string.IsNullOrEmpty(eventTarget))
    {
    return page.FindControl(eventTarget);
    }
    else
    {
    // If __EVENTTARGET is null, the postback control is a button type. Use LINQ
    // to query the Request's form variables order to find the button control.
    var postbackButton = (from x in page.Request.Form.AllKeys
    where page.FindControl(x) is Button || page.FindControl(x.Substring(0, x.Length - 2)) is ImageButton
    select (x.EndsWith(".x") ? x.Substring(0, x.Length - 2) : (x.EndsWith(".y") ? x.Substring(0, x.Length - 2) : x))).FirstOrDefault();

    return (!string.IsNullOrEmpty(postbackButton) ? page.FindControl(postbackButton) : null);
    }
    }
  92. Mark Wingfield 12/22/2009 6:12 AM
    Gravatar
    This worked fine for my drop down list example but a Radio button was a different issue.


    I have a radio button called optListOrderBy that has 2 options. when I clicked one of these, the ctrlname value in GetPostbackControl was set to "optListOrderBy_0". This could not be found on the page and thus, the control value returned was null. I am not sure how to deal with this as it is not dealt with in the button code. Any ideas?
  93. ESR 3/19/2010 3:56 PM
    Gravatar
    Hi,

    by your code we can get postback control, your creating control at design time. I am creating button control dynamically at runtime how should I get that dynamic control ID on postback.
    please let me know the solution.

    thanks,
    ESR
  94. Manoj 3/23/2010 1:57 AM
    Gravatar
    Hi..thank u very much. It helped me a lot. I have two buttons. I want to know buttons's id from the control returned? Is there a way to do it?

  95. DuFFeR 3/24/2010 3:04 AM
    Gravatar
    excellent :)
  96. Deepak Aggarwal 4/7/2010 3:44 AM
    Gravatar
    Excellent Article! Thanks!

    Suggestion to Manoj:
    "Manoj 3/23/2010 1:57 AM
    Gravatar
    Hi..thank u very much. It helped me a lot. I have two buttons. I want to know button's id from the control returned? Is there a way to do it?"

    = Even if you have 10 buttons, you will get nothing for all buttons using aforesaid function except the one which caused postback. So you may apply a conditional logic for that.
  97. Jiss 4/12/2010 10:51 PM
    Gravatar
    Shall i use an asp.net link button like "Request.Form.Get(linkButton1.UniqueID)".?
    I am getting null refrence in my code when i used so.

    but if it is a button, i will get its text.

    Please tell me what i want to do to use link button similiarly?
  98. Sandesh Bhumkar 7/20/2010 11:29 PM
    Gravatar
    Excellent solution for imagebutton. Thank you very much.
  99. 4/1/2011 6:59 AM
    Gravatar
    Identifying control raising postback event &amp;laquo; VikramDoda
  100. 7/4/2011 4:14 PM
    Gravatar
    How to Identify Postback event in Page_Load - Programmers Goodies
  101. 10/20/2011 2:00 AM
    Gravatar
    <br /> ASP.Net &amp;#8211; Fire a button&amp;#8217;s event on the server | Data Milk
  102. 11/25/2011 7:25 AM
    Gravatar
    CREAR FILTRO EN GRIDVIEW (y pintar filas)
  103. 5/28/2012 2:43 PM
    Gravatar
    How can I handle a button event that triggers user control creation? | PHP Developer Resource
  104. 2/26/2013 6:26 AM
    Gravatar
    __eventtarget set to ScriptManager id | Asp.Net developed Tutorials | Asp.Net Developed Tutorials
  105. 7/2/2014 11:11 AM
    Gravatar
    [RESOLVED]page post back or reloaded from same link | ASP Questions &amp;amp; Answers
  106. 7/3/2014 5:24 PM
    Gravatar
    [RESOLVED]Create dynamic controls and button event firing? | ASP Questions &amp;amp; Answers
  107. 7/4/2014 6:51 AM
    Gravatar
    [RESOLVED]page post back or reloaded from same link | ASP Web Form Data Control
  108. 7/5/2014 1:46 PM
    Gravatar
    [RESOLVED]page post back or reloaded from same link | ASP.NET MVC
  109. 7/5/2014 3:57 PM
    Gravatar
    [RESOLVED]How to check which Control made the postback? | ASP Questions &amp;amp; Answers
  110. 7/5/2014 5:18 PM
    Gravatar
    [RESOLVED]using gmail OpenID authentication on website | ASP Questions &amp;amp; Answers
  111. 7/6/2014 7:15 AM
    Gravatar
    [RESOLVED]Create dynamic controls and button event firing? | ASP Web Form Data Control
  112. 7/24/2016 1:53 PM
    Gravatar
    Asp.net How To Find Out What Caused Postback | youinsurance
  113. 10/23/2016 11:33 PM
    Gravatar
    Asp.net How To Tell What Control Caused Postback | ItHowTo
  114. 1/28/2018 5:56 AM
    Gravatar
    On postback, how can I check which control cause postback in Page_Init event - QuestionFocus
  115. 1/28/2019 4:16 AM
    Gravatar
    asp.net - C??mo determinar qu?? bot??n causado devoluci??n
  116. 3/25/2019 9:51 AM
    Gravatar
    asp.net - Comment d??terminer quel bouton caus?? de publication
  117. 2/26/2024 7:38 PM
    Gravatar
    Redirecting
  118. 3/3/2024 3:42 AM
    Gravatar
    OurCBD Life: Your Guide to CBD Health &gt; ??????????????? | ?????????
  119. 3/4/2024 4:42 AM
    Gravatar
    Public profile - CBDone - Aero Sales - Buy, Sell &amp;amp; Rent Aircraft in UK &amp;amp; Europe
  120. 4/3/2024 1:29 PM
    Gravatar
    (IA$17) Mp4 8K Prietenia Rom??ne??c Film Torrent Magnet - Blog de caoshuye888
  121. 4/19/2024 10:23 PM
    Gravatar
    Recensioni da Booking.com
  122. 4/22/2024 4:33 PM
    Gravatar
    Leaving xHamster
  123. 4/24/2024 3:16 AM
    Gravatar
    Activelife Keto: Your Guide to Sustainable Weight Management &gt; ??????????????? | ?????????????????????????????????
  124. 4/26/2024 6:05 AM
    Gravatar
    External Link Warning
  125. 5/1/2024 4:25 PM
    Gravatar
    Peak CBD: Unlocking Your CBD Potential &gt; ??????????????? | ???????????? ????????????
  126. 5/1/2024 8:02 PM
    Gravatar
    External Redirect | Di???n ????n sinh vi??n CNTT Qu???ng Ninh
  127. 5/5/2024 9:18 AM
    Gravatar
    External Redirect | Welcome to the Language Learning Forum at Polydog
  128. 5/6/2024 2:49 PM
    Gravatar
    Juicy Studio: Readability Test
  129. 5/9/2024 11:33 AM
    Gravatar
    Sweet Calm CBD Supplement &gt; ??????????????? | 2020 ????????????
  130. 6/10/2024 2:14 AM
    Gravatar
    Polar Cooling Portable AC: Your Guide to Efficient Cooling Solutions &gt; ??????????????? | ???????????? ?????????????????? ??????????????????
  131. 6/10/2024 12:05 PM
    Gravatar
    Larch Valley, Yoho National Park &amp;raquo; A Photo Design
  132. 6/16/2024 4:07 AM
    Gravatar
    Externe Weiterleitungen | Abnehmen Forum
  133. 6/21/2024 6:01 PM
    Gravatar
    ???@???v???t???????N?m?F?y?[?W
  134. 6/22/2024 12:48 PM
    Gravatar
    Weiterleitung
  135. 7/3/2024 12:36 PM
    Gravatar
    Read Asian House - predcesati
  136. 7/6/2024 9:49 PM
    Gravatar
    Steam Community
  137. 7/7/2024 12:21 PM
    Gravatar
    Keto Lyte: Tips For Maximizing Effectiveness In Your Keto Diet - | ???????????? ???????????????? ????????????
  138. 7/11/2024 2:24 AM
    Gravatar
    (NB*88) Il Settimo Figlio Online Mkv 8K Ita Mega - Flier
  139. 7/15/2024 8:48 AM
    Gravatar
    ??????????????????
  140. 7/20/2024 1:34 PM
    Gravatar
    ?????? ?????? &amp;#8211; ?????????
  141. 7/26/2024 1:25 PM
    Gravatar
    Alpha Drive: The Importance Of Consistency In Supplement Regimen ??? d120 System
  142. 7/29/2024 9:44 AM
    Gravatar
    Lorem ipsum dolor sit amet
  143. 7/29/2024 1:58 PM
    Gravatar
    (Qs+73) 2K Online Avi Folklore: Las Grabaciones En Long Pond Studio Pel??cula - ptitbenjy55
  144. 7/29/2024 7:15 PM
    Gravatar
    Build a profitable and thriving retail business
  145. 7/29/2024 7:16 PM
    Gravatar
    We Love Photography &amp;#8211; Improvement District 9
  146. 7/29/2024 11:54 PM
    Gravatar
    ?????E???`BBS
  147. 7/31/2024 12:12 AM
    Gravatar
    3 ???????? ???????????? ???????? ???????? ???? ???? ?????? ?????????? ???????? ???????????? ???? ???? ?????????? ?????????? &amp;#8211; ?????????? ??????????????
  148. 7/31/2024 12:20 AM
    Gravatar
    Consulting and Speaking Topics | Organizational Performance
  149. 7/31/2024 5:15 AM
    Gravatar
    English books download pdf The Adventurous Eaters Club: Mastering the Art of Family Mealtime by Misha Collins, Vicki Collins 9780062876881 - agudossi
  150. 8/2/2024 4:15 PM
    Gravatar
    ???????????????ml&amp;gt;
  151. 8/15/2024 9:44 AM
    Gravatar
    ????????????
  152. 8/23/2024 6:16 AM
    Gravatar
    Glyco Renew Blood Support: The Science Behind Effective Blood Sugar Management &amp;#8211; ???????? ???????????? ?????????? ?????????? ???????? ????????????
  153. 8/29/2024 2:36 AM
    Gravatar
    Edelsteinschmuck - G??stebuch
  154. 9/17/2024 3:23 PM
    Gravatar
    Neal Sandover - Gitea: Git with a cup of teaml&amp;gt;
  155. 9/20/2024 6:05 PM
    Gravatar
    ????????????????????????: JosefLahey86ml&amp;gt;
  156. 10/5/2024 7:50 PM
    Gravatar
    Keto Rain Gummies: Comparing Keto Snack Options &amp;gt;???????????????ml&amp;gt;
  157. 10/6/2024 8:42 PM
    Gravatar
    doctor | Peti oktobar - dve decenije kasnije
  158. 10/7/2024 1:03 AM
    Gravatar
    Bruger:PhoebeFournier - Ribewiki
  159. 10/7/2024 4:09 AM
    Gravatar
    redirecting...
  160. 10/7/2024 4:45 AM
    Gravatar
    Forumml&amp;gt;
  161. 10/7/2024 7:42 PM
    Gravatar
    Dale Barnet - Touhou devml&amp;gt;
  162. 10/8/2024 6:06 AM
    Gravatar
    MargretF24, Autor em Brech?? Beb??
  163. 10/8/2024 3:34 PM
    Gravatar
    g_een_st_eet_o_igins_cbd:dosage_tips_fo_effective_cbd_use [DokuWiki]
  164. 10/8/2024 9:06 PM
    Gravatar
    Utilisateur:FJNFloyd54 ??? Thomas Wiki
  165. 10/11/2024 10:47 PM
    Gravatar
    Edna Kippax - Gusdya Repositoryml&amp;gt;
  166. 10/12/2024 3:47 PM
    Gravatar
    Bell on Wharf &amp;#8211; Diamoo
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections