I have 2 button controls. When I click one i'm trying to determine which one caused a postback in the page load. How to do determine this?
-
6Why don't you handle the buttons' [click-event](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click.aspx)? Then you would have two different handlers, one for each button. Apart from that: [Determining the Control that Caused a PostBack](http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx) – Tim Schmelter Apr 19 '12 at 14:15
-
1How do you handle postback? By what event? – abatishchev Apr 19 '12 at 14:17
-
@huMptyduMpty - fair point, but you might argue that Tim's comment and my answer are worth preserving in their own right? – CJM Apr 19 '12 at 15:29
5 Answers
What about using CommandName and CommandArgument has shown in this example. This way you can have just one handler.
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>

- 98,240
- 88
- 296
- 433

- 7,304
- 2
- 23
- 26
-
1
-
@Govind: Right. `sender is Button2`, `sender as Button2`, `((Button)sender)).ID` and many other ways. But in general good advise. – abatishchev Apr 19 '12 at 14:32
Do you come from a Classic ASP background? When I first used ASP.NET, the same question occurred to me.
Consider an alternative approach:
Rather than detect the postback in the Form_Load, and then figure out what triggered it, create a specific event handler for each of your buttons. This is the whole point of Web Forms - so you can develop apps in very similar ways as you would Windows applications.

- 11,908
- 20
- 77
- 115
-
1The reason why I want to know from the form load rather than and create a specific event is that im creating a dynamic table, and as you know I have to re-create it every time I do a postback. Well im noticing that its causing my page to slow down, and im seeing in this one instance that when I dont need to use that particular function to re-create that table but I dont know what button/control caused that postback – Will Apr 20 '12 at 12:12
-
In this case, you can use the `Request["__EVENTTARGET"]` method explained above (and mark as the answer) – Darbio May 10 '13 at 01:31
Really input with type button sends its value within post request. For example if you have you'll get in Post button-name=Quote like it's simple text input. So you can just check if post contains value for the button using code like following (sorry for my vb):
Dim isQuote As Boolean = HttpContext.Current.Request.Form(SubmitQuote.UniqueID) IsNot Nothing
so if it's not Nothing (null) then post has been sent by SubmitQuote button.
BTW for me HttpContext.Current.Request("__EVENTTARGET") didn't work either.

- 262
- 4
- 10
In my implementation there are several forms on my page; if a post-back was triggered by certain button controls further operations are necessary.
The controls are of the following type, which do not populate Request["__EVENTTARGET"]
- Button (at the root of the form)
- Image Button (nested within a Datagrid)
I determine if the following button controls instigated the post-back, by reviewing that the UniqueID
of the control was passed to the form request within the Page_Load
sub:
- ASP.NET:
<asp:Button ID="Button1" runat="server" />
<asp:Button ID="Button2" runat="server" />
To simply handle whether the following nested image button instigated the post-back I take advantage of the OnClientClick
attribute which calls to a javascript function that will populate the value of a supplementary hidden field control with the UniqueID
of the instigating control, then review the hidden control value similarly in the Page_Lode
sub:
- ASP.NET:
<script type="text/javascript">
function SetSource(SourceID) {
var hiddenField = document.getElementById("<%=HiddenField1.ClientID%>");
hiddenField.value = SourceID;
}
</script>
<asp:HiddenField ID="HiddenField1" runat="server" Value="" />
<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="SetSource(this.id)" />
The Page_Load
would then implement by some means:
-VB.NET
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
' ...
If Not Page.IsPostBack Then
If Not String.IsNullOrEmpty(Me.Page.Request.Form(Button1.UniqueID)) Then
' ...
ElseIf Not String.IsNullOrEmpty(Me.Page.Request.Form(Button2.UniqueID)) Then
' ...
ElseIf Not Me.Page.Request.Form(HiddenField1.UniqueID) Is Nothing _
And Not String.IsNullOrEmpty(Me.Page.Request.Form(HiddenField1.UniqueID)) Then
' ...
HiddenField1.Value = String.Empty
Else
' ...
End If
End If
End Sub

- 185
- 15
-
1This is the JavaScript method, but an alternative approach would be [this answer](https://stackoverflow.com/a/12273424/2912011). – David Rogers Nov 01 '18 at 19:59
on page load check this
String ButtonID = Request["__EVENTTARGET"];

- 13,627
- 17
- 68
- 94
-
-
1This gives you the `UniqueID` of the object which sent the request. e.g. `button.UniqueID` – Darbio May 10 '13 at 01:25
-