How to determine which button caused postback

2019-04-06 07:48发布

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?

5条回答
做个烂人
2楼-- · 2019-04-06 08:31

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"/>
查看更多
smile是对你的礼貌
3楼-- · 2019-04-06 08:34

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.

查看更多
狗以群分
4楼-- · 2019-04-06 08:40

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
查看更多
我只想做你的唯一
5楼-- · 2019-04-06 08:40

on page load check this

String ButtonID = Request["__EVENTTARGET"];
查看更多
贪生不怕死
6楼-- · 2019-04-06 08:43

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.

查看更多
登录 后发表回答