且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RegisterStartupScript和RegisterClientScriptBlock之间的区别?

更新时间:2023-12-05 22:40:40

RegisterClientScriptBlock方法将脚本块添加到呈现页面的顶部,其中 RegisterStartupScript方法将脚本块添加到呈现页面的末尾.当页面完成加载但在引发页面的OnLoad事件之前执行.

参考:
MSDN:RegisterClientScriptBlock方法 [ MSDN:RegisterStartupScript方法 [
RegisterClientScriptBlock method adds a script block to the top of the rendered page where as RegisterStartupScript method adds a script block to the end of the rendered page. It executes when the page finishes loading but before the page''s OnLoad event is raised.

Refer:
MSDN: RegisterClientScriptBlock Method[^]
MSDN: RegisterStartupScript Method[^]


RegisterStartupScript 放置脚本在asp.net页面的底部而不是顶部.
它使用指示是否添加脚本标签的type, a key, a script literal, and a Boolean value向Page对象注册启动脚本到页面.
RegisterClientScriptBlock 将脚本插入到Page的开始标记正下方.表示在<form>标记之后.
它使用指示是否添加脚本标签的type, key, script literal, and Boolean value向Page对象注册客户端脚本到页面对象.

相似之处:
RegisterStartupScript RegisterClientScriptBlock均由其键和类型唯一标识.
具有相同键和类型的脚本被视为重复脚本.该页面上只能注册具有给定类型和密钥对的一个脚本.尝试注册已注册的脚本不会创建该脚本的副本.
RegisterStartupScript Places the script at the bottom of the asp.net page instead of at the top.
It registers the startup script with the Page object using a type, a key, a script literal, and a Boolean value indicating whether to add script tags.

RegisterClientScriptBlock inserts script immediately below the opening tag of the Page . means after <form> tag.
It registers the client script with the Page object using a type, key, script literal, and Boolean value indicating whether to add script tags.

Similarities:
Both RegisterStartupScript and RegisterClientScriptBlock 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.