views:

1610

answers:

3

If I want to inject a globally scoped array variable into a page's client-side javascript during a full page postback, I can use:

this.Page.ClientScript.RegisterArrayDeclaration("WorkCalendar", "\"" + date.ToShortDateString() + "\"");

to declare and populate a client-side javascript array on the page. Nice and simple.

But I want to do the same from a async postback from an UpdatePanel.

The closest I can figure so far is to create a .js file that just contains the var declaration, update the file during the async postback, and then use a ScriptManagerProxy.Scripts.Add to add the .js file to the page's global scope.

Is there anything simpler? r iz doin it wrong?

A: 

You could also update a hidden label inside the update panel which allows you to write out any javascript you like. I would suggest though using web services or even page methods to fetch the data you need instead of using update panels.

Example: myLabel.Text = "...."; ... put your logic in this or you can add [WebMethod] to any public static page method and return data directly.

typemismatch
Close with the label, but use a literal rather than a label.
Joel Coehoorn
This a better solution, forcing me to go all client-side with the array, event, and control, instead of creating a spaghetti string of server-to-client-to-server-to-client-to-server. Thanks.
Noel
+2  A: 

Use the static method System.Web.UI.ScriptManager.AddStartupScript()

The script will run on all full and partial postbacks.

Thanks for the pointer. This is indeed a better option than what I listed, but it wasn't The True Answer, since it still required me to create and register a javascript block every time I needed to update the array.
Noel
+1  A: 

Sam is correct. ScriptManager.RegisterStartupScript is the correct name of the function . It will run on all full and partial page updates.

Sangeet