Skip to content Skip to sidebar Skip to footer

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 a

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

<scriptlanguage='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;
    functionsaveButtonFunction(){
       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);
    functionDisplayRadAlertHandler() {
        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.

Post a Comment for "How To Unregister Page.clientscript In C# Asp.net"