How to unregister Page.ClientScript in C# Asp.net

我的梦境 提交于 2019-12-11 05:03:41

问题


I am registering java script to my Asp.net code behind file, which is working fine. Now, I have some update panels on the same page and problem is whenever there is any change in any of the update panel, this script is automatically getting called. Is there any way that I can stop this happening. I can't remove update panels from my page and this script is also a very essential part of the application. In this situation I am just calling a alert (rad alert with set time out functionality) when Save Button is clicked or an Update routine is called while I have few other buttons in update panels and whenver any of the button which is registered to the update panels clicked, the following script is called un-willingly. Anyone's help will really be appreciated.

following is my Page.ClientScript

                string radalertscript = "<script language='javascript'> Sys.Application.add_load(function(sender, e) {var oWnd = radalert('dialogMessage', 400, 140, 'Saved');window.setTimeout(function () { oWnd.Close(); }, 3000);});</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);

回答1:


You can assign empty string to same key radalert to remove the script.

if(some_condition)
    Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", "");

Edit: Based on comments, you can make it simple without using RegisterStartupScript

In code behind

btnSave.Attributes.Add("", "saveButtonFunction();");

In Javascript

<script language='javascript'>

   Sys.Application.add_load(function(sender, e) {
           if(btnSaveClicked){
              var oWnd = radalert('dialogMessage', 400,140, 'Saved');
              window.setTimeout(function () { oWnd.Close(); }, 3000);
              btnSaveClicked = false;
           }
    });

    btnSaveClicked = false;
    function saveButtonFunction(){
       btnSaveClicked = true;
    };    

</script>



回答2:


Thank you very much for your answer Adil. I already have followed the same approach with little difference. I have taken JavaScript out from my code behind file and have registered Sys.Application.add_load event as follow

    Sys.Application.add_load(DisplayRadAlertHandler);
    function DisplayRadAlertHandler() {
        var getMessage = document.getElementById('<%=radAlertDialogHidden.ClientID%>').value;
        if (getMessage != "") {
            document.getElementById('<%=radAlertDialogHidden.ClientID%>').value = "";
            var oWnd = radalert(getMessage, 400, 140, 'Saved');
            window.setTimeout(function () { oWnd.Close(); }, 3000);
        }
    }

Here I am setting my alert message in a hidden input field from code behind file and in the above event handler I am just checking if message is there than reset the hidden field and display the message. Your approach is also right and I have marked your answer but as I am displaying my message from multiple locations (Save button, Update routine etc.) so by assigning value to hidden input field and than resetting in above event handler looks more appropriate. Thanks once again for your help.



来源:https://stackoverflow.com/questions/14337214/how-to-unregister-page-clientscript-in-c-sharp-asp-net

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