2

一个小问题,我希望承认一个简单的答案,我会因为没有注意到而踢自己。

因此,当我们有以下 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 框架免于检索运行时类型本身的麻烦。

4

1 回答 1

3

通常 type 是注册脚本的页面或控件的类型。这只是防止两个不同的控件使用相同的键注册不同的脚本的一种方法。

摘自MSDN

客户端脚本由其键和类型唯一标识。具有相同键和类型的脚本被认为是重复的。页面中只能注册一个具有给定类型和密钥对的脚本。尝试注册已注册的脚本不会创建该脚本的副本。

于 2010-07-21T15:46:20.967 回答