43

I have two buttons:

<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" />

How can I determine on pageLoad which one of this two caused the postback? Is there a short solution as I know there are only two controls that can cause this postback?

hhh3112
  • 2,167
  • 10
  • 36
  • 55

5 Answers5

68

You can use this method to get the control that caused the postback:

/// <summary>
/// Retrieves the control that caused the postback.
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
private Control GetControlThatCausedPostBack(Page page)
{
    //initialize a control and set it to null
    Control ctrl = null;

    //get the event target name and find the control
    string ctrlName = page.Request.Params.Get("__EVENTTARGET");
    if (!String.IsNullOrEmpty(ctrlName))
        ctrl = page.FindControl(ctrlName);

    //return the control to the calling method
    return ctrl;
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • This is not finding the Control if its within a repeater since the control name is empty. Any idea how to find this? – djmj Jul 17 '12 at 11:17
  • "Page" in the first Page.Request should be page.Request – LarryBud Nov 22 '13 at 19:06
  • 7
    Returns null in many cases. The proper solution is: http://stackoverflow.com/questions/3175513/on-postback-how-can-i-check-which-control-cause-postback-in-page-init-event – Tillito Jan 28 '14 at 15:53
  • 3
    How does this work for a button, as asked by the OP? `__EVENTTARGET` is just an empty string when the postback is caused by a button – sab669 Aug 20 '15 at 14:23
  • 1
    Note, `page.Request.Params.Get("__EVENTTARGET")` returns unique ID. – Gqqnbig Sep 29 '16 at 01:40
  • is there a way to get the Button control? It does not work on submit buttons... – Alessandro Dec 12 '22 at 10:41
12

http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

private string getPostBackControlName()
    {
        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 ctrlname = Page.Request.Params["__EVENTTARGET"];
        if (ctrlname != null && ctrlname != String.Empty)
        {
            control = Page.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
        {
            string ctrlStr = String.Empty;
            Control c = null;
            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"))
                {
                    ctrlStr = ctl.Substring(0, ctl.Length - 2);
                    c = Page.FindControl(ctrlStr);
                }
                else
                {
                    c = Page.FindControl(ctl);
                }
                if (c is System.Web.UI.WebControls.Button ||
                         c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
            }

        }

        if (control != null)
            return control.ID;
        else
            return string.Empty;
    }
Aristos
  • 66,005
  • 16
  • 114
  • 150
Frank Myat Thu
  • 4,448
  • 9
  • 67
  • 113
2

This helps to find the name of control that caused postback in pageload.This helped me.Hope this helps someone else too

  <asp:Button ID="Button1" runat="server" Text="Button"
  OnClientClick = "SetSource(this.id)" />

   <asp:ImageButton ID="ImageButton1" runat="server"
    OnClientClick = "SetSource(this.id)" />

            <script type = "text/javascript">
            function SetSource(SourceID)
          {
    var hidSourceID =
    document.getElementById("<%=hidSourceID.ClientID%>");
    hidSourceID.value = SourceID;
    }
    </script>

 on code behind you can get the ID of the function using :
 if (IsPostBack)
 {
   string CtrlID = string.Empty;
  if (Request.Form[hidSourceID.UniqueID] != null &&
       Request.Form[hidSourceID.UniqueID] != string.Empty)
    {
     CtrlID = Request.Form[hidSourceID.UniqueID];
   }
}
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Linta Sheelkumar
  • 195
  • 1
  • 5
  • 21
2

To add to this: you can find which event caused the postback with:

Page.Request.Params.Get("__EVENTARGUMENT")
Yelnic
  • 541
  • 2
  • 6
  • 6
-1
protected void ReUsedMethod_Click(object sender, EventArgs e)
{
  string ctrlName = ((Button)sender).ID;
}
boateng
  • 910
  • 11
  • 21