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

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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>

Solution 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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply