How to determine which button caused a postback

I have 2 control buttons. When I click one, I try to determine which one caused the postback in the page load. How to determine this?

+7
source share
5 answers

How about using CommandName and CommandArgument shown in this example . This way you can have only 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"/> 
+3
source

Did you come from a classic ASP background? When I first used ASP.NET, the same question arose with me.

Consider an alternative approach:

Instead of detecting the postback in Form_Load and then figuring out what caused it, create a special event handler for each of your buttons. This is the whole point of web forms - so you can develop applications in the same way as Windows applications.

+2
source

check it on the download page

 String ButtonID = Request["__EVENTTARGET"]; 
+1
source

Indeed, input with a type button sends its value to the mail request. For example, if you have, you will receive the message "Mail button-name = Quote", as a simple text entry. So you can just check if the message contains a value for the button using the following code (sorry for my vb):

Dim isQuote As Boolean = HttpContext.Current.Request.Form(SubmitQuote.UniqueID) IsNot Nothing

so if it is not Nothing (null), then the message is sent by the SubmitQuote button.

By the way, for me, HttpContext.Current.Request ("__ EVENTTARGET") did not work either.

+1
source

In my implementation, there are several forms on my page; If a post-back call was made by some control buttons, further operations are necessary.

The controls are of the following type, which does not populate Request["__EVENTTARGET"]

  • Button (at the root of the form)
  • Image Button (nested in Datagrid)

I determine if the following buttons initiated the post-back buttons by seeing that the UniqueID element of the control was passed to the form request in the Page_Load sub:

 - ASP.NET: <asp:Button ID="Button1" runat="server" /> <asp:Button ID="Button2" runat="server" /> 

To simply handle whether the next button of the attached image has called post-back, I use the OnClientClick attribute, which calls the javascript function, which will populate the value of the additional hidden field with the UniqueID element and then look at the hidden control value in the same way in 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)" /> 

Page_Load will then be implemented in some ways:

 -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 
0
source

All Articles