Search code examples
c#asp.netjavascriptclientscript

Why can't you UnRegisterStartupScript?


Seems to me it is a bit wierd that you can do..

Page.ClientScript.RegisterStartupScript(this.GetType(), "KeyName", "alert('changed my mind')", true);

And then later on you can't unregister or stop the javascript from being rendered programatically.

Why would Microsoft do this?

I don't like the work around here.. http://hemant-vikram.blogspot.com/2005/11/unregister-startup-script-workaround.html

And I don't like the option of just re-registering it and having it do nothing..

Thoughts?


Solution

  • I assume that you'd want to "unregister" the script (which has already been registered) under some condition, like so:

    Page.ClientScript.RegisterStartupScript(this.GetType(), "KeyName", "alert('changed my mind')", true);
    ...
    if(condition)
        Page.ClientScript.UnregisterStartupScript(this.GetType(), "KeyName", "alert('changed my mind')", true);
    

    Why can't this simply be changed to:

    if(!condition)
        Page.ClientScript.RegisterStartupScript(this.GetType(), "KeyName", "alert('changed my mind')", true);