9

How can I check whether a particular button was clicked or not in ASP.NET?

I think I need to perform some operation on Page_Load. This shouldn't be entering to Button_Click event to find. Is there any way that I can find where it was clicked or not on Client Side and take it to Page_Load?

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
user1030181
  • 1,995
  • 5
  • 26
  • 56
  • Why do you think you need to do in on `Page_Load`? Why can you not handle it on button click? – Jon P Jan 13 '15 at 05:57

5 Answers5

16

Background: Basically __EVENTTARGET and __EVENTARGUMENT , These two Hidden controls are added to the HTML source, when ever any autopostback attribute is set to true for any of the web control.

The __EVENTTARGET hidden variable will tell the server ,which control actually does the server side event firing so that the framework can fire the server side event for that control.

The __ EVENTARGUMENT variable is used to provide additional event information if needed by the application, which can be accessed in the server.

So we can easily get the control causing postback using:Request.Params.Get("__EVENTTARGET");

PROBLEM:

The method: Request.Params.Get("__EVENTTARGET"); will work for CheckBoxes, DropDownLists, LinkButtons, etc.. but this does not work for Button controls such as Buttons and ImageButtons

The Button controls and ImageButton controls does not call the __doPostBack function. Because of this, the _EVENTTARGET will always be empty. However, other controls uses javascript function __doPostBack to trigger postback.

So, I will suggest to do something as below. Add an OnClientClick property to the buttons. Also, define a hiddenField in your Markup, whose value will contain the actual button causing postback.

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

On the OnClientClick property of the Button and ImageButton Call the SetSource JavaScript function

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

Here onwards, you can very easily check in your Page_Load as to which Control caused postback:

if (IsPostBack)
{
    string CtrlName;
    CtrlName=hidSourceID.Value;
}
R.C
  • 10,417
  • 2
  • 35
  • 48
2

I just got the same trouble, have to do some logic judgement in the Page_Load method to treat different event(which button was clicked). I realize the arm to get the as the following example. The front end aspx source code(I have many Buttons with IDs F2, F3, F6, F12.

<Button Style="display: none" ID="F2" runat="server" Text="F2:Cancel" OnClientClick="SeiGyo(this)" OnClick="F2_Click" />
    <Button Style="display: none" ID="F3" runat="server" Text="F3:Return" OnClientClick="SeiGyo(this)" OnClick="F3_Click" />
    <Button Style="display: none" ID="F6" runat="server" Text="F6:Run" OnClientClick="SeiGyo(this)" OnClick="F6_Click" />
    <Button Style="display: none" ID="F12" runat="server" Text="F12:Finish" OnClientClick="SeiGyo(this)" OnClick="F12_Click" />

The back end aspx.cs source code, what I need to do is judge which button was clicked when Page_Load was triggered. It seems a little stupid, but works. In your situation, the button be clicked will be added into dic. I hope that will be helpful to some one.

Dictionary<string, string> dic = new Dictionary<string, string>();
foreach(var id in new string[]{"F2","F3","F6","F12"})
{
       foreach (var key in Request.Params.AllKeys)
                    {
                        if (key != null && key.ToString().Contains(id))
                            dic.Add(id, Request[key.ToString()].ToString()); 
                    }
}
Wang Jijun
  • 326
  • 4
  • 10
  • If you're using , it should be sufficient to loop over Request.Form.AllKeys rather than Request.Params.AllKeys. You can also filter out keys that start with "__" to save a little time. Only the clicked button's ID will appear among the Form keys. – Steve Lautenschlager Apr 19 '18 at 16:01
0

The UniqueID of the button will be in Request.Form["__EVENTTARGET"]

This question is already answered at: ASP.NET : Check for click event in page_load

Community
  • 1
  • 1
backslash17
  • 5,300
  • 4
  • 31
  • 46
  • 4
    not sure if things have changed, but no, the ASP.NET Button UniqueID does not appear in __EVENTTARGET for me. Which seems to validate what FlopScientist says above -- that this only works for "CheckBoxes, DropDownLists, LinkButtons, etc.. but this does not work for Button controls such as Buttons and ImageButtons" – mdelvecchio Feb 06 '15 at 17:40
0

You can try using the hidden field. Make the client side event on the OnclientClick event and try setting the value of hidden field, may be true or false depending on the condition. And on the page load you can check the value of Hiidden field.

function click()
{
// set the hidden field here
}

And on the page load, simply check the value.

if(HiddenFieldName.Value=="true")
{
//perform the action
}
Incredible
  • 3,495
  • 8
  • 49
  • 77
-1
    private bool button1WasClicked = false;

    private void button1_Click(object sender, EventArgs e)
    {
        button1WasClicked = true;
    }


if ( button1WasClicked== false)
{
//do somthing
}
Karthik_SD
  • 633
  • 1
  • 6
  • 22