Simplest way to update a client-side javascript array variable during a ASP.NET AJAX postback in an UpdatePanel?

StackOverflow https://stackoverflow.com/questions/95005

  •  01-07-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

The script will run on all full and partial postbacks.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top