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

Learning Curve picture Learning Curve · Jan 15, 2013 · Viewed 11.9k times · Source

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

Answer

Adil picture Adil · Jan 15, 2013

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>