Monday, October 10, 2011

ClientScript.RegisterStartupScript Not Working?

ClientScript.RegisterStartupScript is used to call javascript in the .net code-behind. Once you learn this nice little trick, you think it is great, until one day years later on another page/project you try it again, but it does not work.

First, please check if the control/page you are trying to update contains an UpdatePanel. If so, keep reading.

This WON'T work with an UpdatePanel (in my experience). You will need to use: ScriptManager.RegisterStartupScript

For example:
ScriptManager.RegisterStartupScript(this, this.GetType(), "saving", "doneSaving();", true);

Also, in VS, it did not seem to recognize ScriptManager, when I right-clicked to "Resolve" it gave me no options. I am not sure why but I had to add "using System.Web.UI;" to my using clauses manually. Maybe that was some quirk with my page though...