调用ScriptManager.RegisterStartupScript时,'Type'参数的重点是什么?

时间:2010-07-21 15:43:27

标签: asp.net-2.0 scriptmanager

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

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

1 个答案:

答案 0 :(得分:2)

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

摘自MSDN

  

唯一标识客户端脚本   按键及其类型。脚本用   考虑相同的密钥和类型   重复。 只有一个脚本   给定的类型和密钥对可以   注册与页面。尝试   注册已经存在的脚本   注册不会创建副本   的剧本。