Duplicate buttons in Update Panel and JavaScript Hack

三世轮回 提交于 2019-12-24 13:04:11

问题


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();
   }
}


来源:https://stackoverflow.com/questions/13533388/duplicate-buttons-in-update-panel-and-javascript-hack

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