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.




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


    • 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?


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

        function ShowMessages(msg1, 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!!

  5. I have “no overload for method ‘registerstartupscript’ takes 5 arguments” when I tried to add “this” in.

      • protected void cmdSave_Click(object sender, System.EventArgs e)
        string key = “_OpenAddrCandidatesWin”;
        string script = “OpenAddrCandidatesWin();”;
        ScriptManager.RegisterStartupScript(this, this.GetType(), key, script, true);

        Sorry I misread the ScriptManager with the ClientScript, I don’t have the error of “take 5 arguments” any more. However, the jQuery dialog is still not firing.

  6. This is my jquery: But it nerver gets to here. It passes the ScriptManager line but never fires the jquery function

    function OpenAddrCandidatesWin() {

    resizable: true,
    width: 650,
    heigh: 450,
    modal: true,
    show: ‘Transfer’,
    hide: ‘Transfer’
    //$(“.selector”).dialog({ dialogClass: ‘no-close’ });

  7. Hi Brij, I traced out the issue, before the Save button is click the form has set AutoPostBack = true, that is why the ScriptManager.RegisterStartupScript is not firing. Below is my asp code to to trigger the jQuery, what should I change to make the RegisterStartupScript work , thanks

    • Good that found the issue. Now you need to stop the postback. For that return false from your JavaScript function like

      function yourmethod( )
      // Do whatever you want to do
      return false;

      and use it at your button as

      onclick=”javascript: return yourmethod();”
      This should resolve your issue.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s