Duplicate buttons in Update Panel and JavaScript H

2019-07-28 03:04发布

问题:

In reference to my question,

Page.Unload Event inside a Update Panel

I am able to make it work but I got two buttons that do partial post back and then in my script I can do what I want, but I want it to work for just one button not two of them, best thing is button I want to make this request work for is Image button whereas other button is just a simple button. This is a User Control in ASP.NET by the way.

    <script type="text/javascript"> 
   // if you use jQuery, you can load them when dom is read.
   $(document).ready(function () {
       var prm = Sys.WebForms.PageRequestManager.getInstance();    
       prm.add_initializeRequest(InitializeRequest);
       prm.add_endRequest(EndRequest);

    });        

    function InitializeRequest(sender, args) {
    }

    function EndRequest(sender, args) {
       // after update occur on UpdatePanel run the code.
       UnloadMsgBox();
    }

    function UnloadMsgBox()
    {
        alert("Message");
    }
    </script>

回答1:

Say you have two methods, one called button1_Click and another called imageButton1_Click. We add a HiddenField to the page like so:

<asp:HiddenField runat="server" ID="hidIsTheButton" value="0" />

In our button1_Click method:

hidIsTheButton.Value = "0";

In our imageButton1_Click method:

hidIsTheButton.Value = "1";

Here, button1_Click refers to your standard buttons event handler and imageButton1_Click referes to your ImageButton's event handler.

In your EndRequest javascript function:

function EndRequest(sender, args) {
   // after update occur on UpdatePanel run the code.
   var hidIsTheButton = document.getElementById('<%=hidIsTheButton.ClientID%>');
   if (hidIsTheButton.Value == "1") {
       UnloadMsgBox();
   }
}