一个小问题,我希望能有一个简单的答案,我会因为没有注意到而惩罚自己。
所以,当我们有以下 RegisterStartupScript 重载时
public static void RegisterStartupScript(
Control control,
Type type,
string key,
string script,
bool addScriptTags
)
我们必须提供类型和控件。现在,我可以明白指定控件的意义所在 - 脚本会被推出,以防控件是部分页面渲染的一部分。但是 Type 参数有什么意义呢?通常只需将其设置为控件的类型即可。这实际上是MSDN提出的建议:
control
Type: System.Web.UI..::.Control
The control that is registering the client script block.
type
Type: System..::.Type
The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.
那么为什么我们必须指定它呢?大概不仅仅是为了让 .Net Framework 免去检索运行时类型本身的麻烦。
最佳答案
通常类型是注册脚本的页面或控件的类型。这只是防止两个不同的控件使用相同的 key 注册不同的脚本的一种方法。
摘自MSDN :
A client script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.
关于asp.net-2.0 - 调用ScriptManager.RegisterStartupScript时, 'Type'参数有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3301140/