一个小问题,我希望承认一个简单的答案,我会因为没有注意到而踢自己。
因此,当我们有以下 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 框架免于检索运行时类型本身的麻烦。