How to determine which button caused postback

半城伤御伤魂 提交于 2019-12-04 02:46:38
Kris Krause

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"/>

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.

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.

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

on page load check this

String ButtonID = Request["__EVENTTARGET"];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!