0
votes

In asp.net (c#) how can I find out which asp:button triggered the postback?

I am using it for dynamic controls and want to do a different process on pageload for different buttons. I have tried looking at __EVENTARGUMENTS etc. but they didnt work.

I am looking to do something like this:

Page_load Case: Button 1 clicked //Do something Button 2 clicked //Do something

1
Why not use button click events directly? - Grant Thomas
Ok. One alternative is when you click on any button set unique value in hiddenfield in javascript side. Then on load check for that unique value and according to do as per your requirements. - Harshil Raval
You could also store something in the ViewState, but I think @Grant has the right idea here. A lot of my pages are if(IsPostback) do nothing, else Load Initial Page and there's a real main method which many, but not all, of the event methods call. - Drew

1 Answers

-2
votes

Use the below code.

  public static string GetPostBackControlId(this Page page)
        {
            if (!page.IsPostBack)
                return string.Empty;

            Control control = null;
            // first we will check the "__EVENTTARGET" because if post back made by the controls
            // which used "_doPostBack" function also available in Request.Form collection.
            string controlName = page.Request.Params["__EVENTTARGET"];
            if (!String.IsNullOrEmpty(controlName))
            {
                control = page.FindControl(controlName);
            }
            else
            {
                // if __EVENTTARGET is null, the control is a button type and we need to
                // iterate over the form collection to find it

                // ReSharper disable TooWideLocalVariableScope
                string controlId;
                Control foundControl;
                // ReSharper restore TooWideLocalVariableScope

                foreach (string ctl in page.Request.Form)
                {
                    // handle ImageButton they having an additional "quasi-property" 
                    // in their Id which identifies mouse x and y coordinates
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        controlId = ctl.Substring(0, ctl.Length - 2);
                        foundControl = page.FindControl(controlId);
                    }
                    else
                    {
                        foundControl = page.FindControl(ctl);
                    }

                    if (!(foundControl is Button || foundControl is ImageButton)) continue;

                    control = foundControl;
                    break;
                }
            }

            return control == null ? String.Empty : control.ID;
        }

Calling this function:

I have included the above function in a static class UtilityClass.

String postBackControlId = UtilityClass.GetPostBackControlId(this);

The code has been referenced from Mahesh's Blog.