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:
如果我想在整页回发期间将全局范围的数组变量注入页面的客户端javascript,我可以使用:
this.Page.ClientScript.RegisterArrayDeclaration("WorkCalendar", "\"" + date.ToShortDateString() + "\"");
to declare and populate a client-side javascript array on the page. Nice and simple.
在页面上声明并填充客户端javascript数组。很好,很简单。
But I want to do the same from a async postback from an UpdatePanel.
但我想从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.
到目前为止,我能想到的最接近的是创建一个只包含var声明的.js文件,在异步回发期间更新文件,然后使用ScriptManagerProxy.Scripts.Add将.js文件添加到页面的全局范围。
Is there anything simpler? r iz doin it wrong?
还有什么更简单的吗?我错了吗?
3 个解决方案
#1
0
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.
您还可以更新更新面板中的隐藏标签,它允许您写出您喜欢的任何JavaScript。我建议使用Web服务甚至页面方法来获取您需要的数据,而不是使用更新面板。
Example: myLabel.Text = "...."; ... put your logic in this or you can add [WebMethod] to any public static page method and return data directly.
示例:myLabel.Text =“....”; ...将您的逻辑放入此中,或者您可以将[WebMethod]添加到任何公共静态页面方法并直接返回数据。
#2
3
Use the static method System.Web.UI.ScriptManager.AddStartupScript()
使用静态方法System.Web.UI.ScriptManager.AddStartupScript()
The script will run on all full and partial postbacks.
该脚本将在所有完整和部分回发上运行。
#3
2
Sam is correct. ScriptManager.RegisterStartupScript is the correct name of the function . It will run on all full and partial page updates.
山姆是对的。 ScriptManager.RegisterStartupScript是函数的正确名称。它将在所有完整和部分页面更新上运行。
#1
0
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.
您还可以更新更新面板中的隐藏标签,它允许您写出您喜欢的任何JavaScript。我建议使用Web服务甚至页面方法来获取您需要的数据,而不是使用更新面板。
Example: myLabel.Text = "...."; ... put your logic in this or you can add [WebMethod] to any public static page method and return data directly.
示例:myLabel.Text =“....”; ...将您的逻辑放入此中,或者您可以将[WebMethod]添加到任何公共静态页面方法并直接返回数据。
#2
3
Use the static method System.Web.UI.ScriptManager.AddStartupScript()
使用静态方法System.Web.UI.ScriptManager.AddStartupScript()
The script will run on all full and partial postbacks.
该脚本将在所有完整和部分回发上运行。
#3
2
Sam is correct. ScriptManager.RegisterStartupScript is the correct name of the function . It will run on all full and partial page updates.
山姆是对的。 ScriptManager.RegisterStartupScript是函数的正确名称。它将在所有完整和部分页面更新上运行。