16

In c#, how can I check to see if a link button has been clicked in the page load method?

I need to know if it was clicked before the click event is fired.

user13053
  • 243
  • 1
  • 2
  • 8
  • If your code is reliant on the OnClick event, then you can put that particular code in the OnPageRender override (rather than in Page_Load), as it will fire after the OnClick event handler. – snumpy Aug 12 '15 at 13:32

6 Answers6

25
if( IsPostBack ) 
{
    // get the target of the post-back, will be the name of the control
    // that issued the post-back
    string eTarget = Request.Params["__EVENTTARGET"].ToString();
}
Jared
  • 8,390
  • 5
  • 38
  • 43
4

if that doesn't work.Try UseSubmitBehavior="false"

RealSteel
  • 1,871
  • 3
  • 37
  • 74
2

Check the value of the request parameter __EVENTTARGET to see if it is the id of the link button in question.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
2

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

Tsvetomir Tsonev
  • 105,726
  • 5
  • 27
  • 34
1

Based on accepted answer and the answer of RealSteel this could be a more complete answer.

First add to the .aspx a button like this:

<asp:Button id="btnExport" runat="server" Text="Export" UseSubmitBehavior="false"/>

Then on the Page_Load method:

if(IsPostBack){
   var eventTarget = Request.Params["__EVENTTARGET"]
   // Then check for the id but keep in mind that the name could be 
   // something like ctl00$ContainerName$btnExport 
   // if the button was clicked or null so take precautions against null
   // ... so it could be something like this
   var buttonClicked = eventTarget.Substring(eventTarget.LastIndexOf("$") + 1).Equals("btnExport")

}
Miguel
  • 1,575
  • 1
  • 27
  • 31
0

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. I hope that will be helpful to some others

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