c# - 如何订购 ScriptManager 的 Register Script 注册的脚本

标签 c# asp.net

我想提醒 1 按此顺序注册脚本

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert(a);", true);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "b", "var a = 1;", true);

但是这样做会导致错误提示 a 未定义。

我有不同的控件位于页面注册脚本的不同部分,其中一些需要先进行,而另一些需要最后进行。

有没有办法设置渲染顺序?

最佳答案

不,不可能定义 RegisterClientScriptBlock 的顺序方法被调用。来自documentation对于 ScriptManager.RegisterClientScriptBlock方法(强调我的):

The script blocks are not guaranteed to be output in the order they are registered. If the order of the script blocks is important, use a StringBuilder object to gather the scripts together in a single string, and then register them all in a single client script block.

此外,您可能想为 "a" 的第二次调用定义不同的键(第二个参数;RegisterStartupScriptBlock) ,因为注册的脚本由其 key 唯一标识。如果您使用参数调用此方法,ScriptManager不会注册第二个脚本,因为已经注册了具有相同 key ( "a" ) 的另一个脚本。

关于c# - 如何订购 ScriptManager 的 Register Script 注册的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3450325/

相关文章:

c# - xxx 不是有效的虚拟路径?

c# - 类构造函数错误

c# - 找不到可安装的 ISAM

c# - 在不知道年份的情况下使用 DateTime.TryParseExact

c# - 当一切都在编译时已知时,为什么我不能将 nameof 与泛型一起使用?

c# - 如何从 Windows 8.1 中的导航堆栈中删除后退条目?

c# - 在 ASP.NET CORE 中的 Startup.cs 中设置动态变量

javascript - 使用 GetElementById 传递多个值(在特定字符后拆分)

c# - 如何使用 C# 反序列化 JSON

c# - 为什么在 C# 中看不到 Ping 类?