asp.net-2.0 - 调用ScriptManager.RegisterStartupScript时, 'Type'参数有什么意义?

标签 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 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/

相关文章:

asp.net - 如何在母版页中包含 CSS?

validation - 如何在没有日期的时间使用 CompareValidator?

ASP.NET 子窗口在每次回发时都会在新窗口上打开

iis - 使用压缩动态内容时如何禁用分块传输编码?

asp.net - ScriptManagers 的 ScriptMode 中的 Debug 和 Release 有什么区别?

c# - 使用 C# 获取本地和网络打印机的列表

asp.net - 回发破坏了我的 GridView 列中的用户控件

c# - 如何从 Chrome 中的 url 中清除哈希

javascript - 如何在部分回发中保留脚本 block ?