ClientScriptManager 和 ScriptManager RegisterClientScriptBlock
ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) Method
Registers an OnSubmit statement with the Page object using a type, a key, and a script literal. The statement executes when the HtmlForm is submitted.
RegisterStartupScript
Registers a startup script block with the ScriptManager control and adds the script block to the page.
Registers the startup script with the Page object.
这里的注册脚本,是会运行的。所以可以用来绑定事件。
private void RegisterSaveButtonSubmit()
{
string script = @"
$('.blueimp-file-upload-submit').on('click',function() {
alert('blueimp-file-upload-submit');
});";
var key = "RegisterSaveButtonSubmit";
var type = GetType();
var scriptManager = Page.ClientScript;
if (!scriptManager.IsStartupScriptRegistered(key))
{
scriptManager.RegisterStartupScript(type, key, script, true);
}
}
ScriptManager.RegisterPostBackControl
Registers a control as a trigger for a postback.
This method is used to configure postback controls inside an UpdatePanel control that would otherwise perform asynchronous postbacks.
RegisterClientScriptBlock
Registers the client script with the Page object.
Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.
需要注意的是,这里注册的脚本是不会运行的。不能是jQuery的绑定事件,不会触发
the most important part is Control which control in html tags you want to register the script for example if you have user control and you want to run the script just for that use this line
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertscript", "document.getElementById('userControl_h1TAG')", true);
but when you want to register the block and script to all part of that page use this line in CS code of user-control :
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "alertscript", "document.getElementById('page_h1TAG')", true);
There a post on why this could lead to trouble, but I've never actually encountered this. It comes down to this: when you inherit from a control that has this piece of code, the GetType will return something else. This way, the key will differ and the script will be added a second time if you have both controls on your page. This could potentially lead to javascript problems.
The solution would be to not use GetType but typeof() instead. In VB.Net:
Page.ClientScript.RegisterClientScriptBlock(GetType(MyClass), "key","scriptblock", True)
But again, this is an exceptional case.
When using RegisterClientScript do not use this.GetType() …
Quoted from: http://blogs.ipona.com/james/archive/2006/10/03/6710.aspx
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “Script”, scriptText);This is not good. Why not? Well, this.GetType() is a way of getting the runtime type of an object. That’s not necessarily the same type as the one in which this bit of code is being declared.So what? Does that actually make any difference? Well, yes, if anyone ever writes a control that inherits from your control.If I create a control called Widget that calls RegisterClientScriptBlock() passing this.GetType(), then whenever I put a few Widget controls on the page, the script block will be registered once, and only once. That’s great.Then later on, I develop a SpecialisedWidget control that inherits from Widget. I drop it onto the page, and suddenly, RegisterClientScriptBlock is getting called with the same script, but two different types – Widget, and SpecialisedWidget. The script block ends up appearing on the page twice. Cue tricky hard to track JavaScript bugs that take ages to find…Now, if Widget had originally registered that script block passing typeof (Widget) as the type argument instead of this.GetType(), the whole problem wouldn’t arise, because even when the SpecialisedWidgets are registering script on the page, the script is registered with the type qualifier of Widget. And as a side bonus, the IL will be more efficient, because typeof (Widget) is a kind of type literal, which the compiler can embed right into the code, rather than a runtime dispatched method call to a reflection API.
RegisterStartupScript和RegisterClientScriptBlock区别(ZZ)
ClientScriptManager 和 ScriptManager RegisterClientScriptBlock的更多相关文章
- ClientScriptManager与ScriptManager向客户端注册脚本的区别
使用ClientScriptManager向客户端注册脚本 ClientScriptManager在非异步(就是说非AJAX)环境下使用的.如果要在异步环境下注册脚本应该使用ScriptManager ...
- ASP.NET后台输出js大全,页面顶部、form表单中前面与后面、和UpdatePanel(ScriptManager、AJAX)输出JS
Response.Write 与 Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...
- 弹出消息对话框ScriptManager
//直接调用WebMessageBox方法 #region 弹出消息对话框 /// <summary> /// 弹出消息对话框 /// </summary> /// <p ...
- RegisterStartupScript和RegisterClientScriptBlock的区别
1. //注册到 <form> 尾部 ,HTML元素已加载完毕 this.Page.ClientScript.RegisterStartupScript(this.GetType( ...
- ScriptManager的几个属性和方法
ScriptManager的几个属性和方法 一.EnablePageMethods ScriptManager的EnablePageMethods属性用于设定客户端javascript直接调用服务 ...
- C#中js文本提示
Page.ClientScript 与 ClientScript 的关系 这二者实际上是一个东西,后者只是省略了 Page.都是获取用于管理脚本.注册脚本和向页添加脚本的 ClientScriptMa ...
- 【转载】asp.net 后台弹出提示框
感觉这种最好用: public void showMessage(string str_Message) { ClientScript.RegisterStartupScript(this.GetTy ...
- .Net中使用无闪刷新控件时提示框不显示
今天做提示框的时候一直不显示,让我郁闷好久,晚上吃饭的时候问了同事一下,他给了一个思路, 他说可能是因为由于页面中的无闪刷新导致的结果:百度了一下真找到了解决方法 在页面中存在无闪刷新控件的时候提示框 ...
- cPage分页,asp.net自定义分页,url传值分页,支持datalist、gridview、Repeater等
asp.net分页是最最常用的功能,实现方式也很多,使用不同的控件有不同的分页方式. 下面分享一个我们团队内部使用了多年的一个分页控件cPage,是自己设计编写,没有冗余,简单.快速. cPage,现 ...
随机推荐
- 部署k8s集群之环境搭建和etcd单节点安装
环境搭建以及etcd 单节点安装过程 安装之前的环境搭建 在进行k8s安装之前先把虚拟机准备好,这里准备的是三台虚拟机 主机名 ip地址 角色 master 172.16.163.131 master ...
- springboot(二十二)-sharding-jdbc-读写分离
前面我们使用sharding-jdbc配置了分库分表.sharding-jdbc还有个用法,就是实现读写分离. 什么时候需要或者可以使用读写分离? 当我们的项目所使用的数据库查询的访问量,访问频率,及 ...
- apply,call 和 bind 有什么区别
三者都可以把函数应用到其他对象上,不是自身对象,apply,call是直接执行函数调用,bind是绑定,执行需要再次调用,apply和call的区别是apply接受数组作为参数,而call是接受逗号分 ...
- 常用的商业级和免费开源Web漏洞扫描工具
Scanv 国内著名的商业级在线漏洞扫描.可以长期关注,经常会有免费活动.SCANV具备自动探测发现无主资产.僵尸资产的功能,并对资产进行全生命周期的管理.主动进行网络主机探测.端口探测扫描,硬件特性 ...
- ISO/IEC 15444-12 MP4 封装格式标准摘录 2
目录 Track Media Structure Media Box Media Header Box Handler Reference Box Media Information Box Medi ...
- Spring笔记之IOC
本篇笔记忽略jar包的导入和配置文件的schema约束 1.我理解的IOC ioc,控制反转,在spring中我理解的ioc就是将需要创建的对象交由spring来创建.在spring中,可以通过配置, ...
- 02-spring框架—— IoC 控制反转
控制反转(IoC,Inversion of Control),是一个概念,是一种思想.指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理. 控制反转就是对对象控制权的转移 ...
- 关于Vue-$router传参出现刷新页面或者返回页面丢失数据的问题
也算是踩到坑了,但不是我踩到的,不过还是得说下这个问题,很严重,对于小白和初学者是比较有帮助的,如果使用到路由传参,请选择你想要的传参方式params或者query 1.query this.$rou ...
- 标准C语言(1)
C语言程序的绝大部分内容应该记录在以.c作为扩展名的文件里,这种文件叫源文件,C语言里还包含以.h作为扩展名的文件,这种文件叫做头文件 C语言程序里可以直接使用数字和加减乘除四则运算符号(*代表乘法, ...
- AIX 下的 find 命令使用
平常我们使用 find , -size +100M/K/G ,就可以找到相应大小的文件了. 可是 AIX 平台下,却好像不能使用,虽然执行起来不报错,但是查找出来的文件却并不是我们想要的.所以 m ...