Page.ClientScript.RegisterStartupScript is not working : A Solution

This is going to be a very short post but will be useful many developers.

You have registered some JavaScript block from server side. But it’s not working. Actually it’s not getting rendered on Client side when you see the PageSource. But the same code works in different projects/applications/pages. Say you have code like this

           Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('Record is not updated');", true);
 

It is registering one one startup script that will be fired when the page will be loaded.
Say you have used at several time and it was working but now it is failing.  Similarly Page.ClientScript provide many other methods that will also not work.

One of the main reasons is, We start using Ajax on our pages. Ajax requires a ScriptManager to handle all the ajax related (Partial postback) tasks. Ajax requires lots of JavaScript code that is managed by the ScriptManager. And it must be noted on a single page only one instance of ScriptManager is allowed.

So whenever we have an instance of  ScriptManager on our page, we need to register the Script using ScriptManager. So you need to change the code like this

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowStatus", "javascript:alert('Record is not updated');", true);

       

Now this code make the script start working. Similarly , we also need to change other Page.ClientScript methods to appropriate ScriptManager method.

Cheers,

Brij

About these ads

22 thoughts on “Page.ClientScript.RegisterStartupScript is not working : A Solution

  1. HJi Brij,

    (1) How will you pass a message from server to a javascript alert box? For eg. after a database updation i want to show a update success alert box meassge to the client.
    (2) When using ‘Page.ClientScript.RegisterStartupScript…’ , suppose i want to call the alertbox twice consecutively, then should i have to call ‘Page.ClientScript.RegisterStartupScript…’ twice?
    It doesnt seem to work the second time.

    thanks
    ben

    • 1) There are several ways-
      – You can directly register a script as I did in my above post. You can directly put your custom message based on some logic instead of ‘Record is not updated’.
      – Other way, create a message at server side, assign it to some hidden variable and then at Client side while get the message from hidden variable and show it in alert box.

      2) You cannot call it twice, instead write code for alert twice in same script

      • Hello Brij
        Thanks for the help.
        2) You cannot call it twice, instead write code for alert twice in same script
        Can you please give an example how tits done?

        ben

      • I mean to say, registering a script two times would not work. You can write a javascript function like

        function ShowMessages(msg1, msg2) {
        alert(msg1);
        alert(msg2);
        }

        You can write your javascript method at your aspx page or can write in a javascript file and include it. Then register it at cs page as
        ScriptManager.RegisterStartupScript(this, this.GetType(), “ShowStatus”, “javascript:ShowMessages(‘First Message’,’Second Message’);”, true);

        Hope it’ll will clear the idea..

  2. Thank you! This exact issue was driving me crazy!! I use an AjaxScriptManager but was still using “Page.ClientScript” to register client script.

  3. I was really struggling with how to add dynamic javascript to my server-side events with an UpdatePanel on the page and this got it working for me. Thanks for a great post.

  4. thanks a lot !
    I am programming a webform project, and when I add a update panel in aspx,
    then Page.ClientScript.RegisterStartupScript just do not work, but if I change it to
    ScriptManager.RegisterStartupScript, then every thing is OK!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s