我想提醒 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/