Page.ClientScript.RegisterStartupScript is not working : A Solution
November 24, 2011 16 Comments
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

Code Projetc Mentor
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..
Thank you! This exact issue was driving me crazy!! I use an AjaxScriptManager but was still using “Page.ClientScript” to register client script.
Thanks a lot it was help me.
Thanks, this solved my problem!
Thanks, it helps me so much.
thanks buddy
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.
Glad to know that It helped you.
Thank you
Thanks a lot
Awesome… Pretty straight and simple. Works like a bomb.
Thanks for your kind words!!
thanks! that helped me.