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

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.registerstartupscript?view=netframework-4.8

Registers a startup script block with the ScriptManager control and adds the script block to the page.

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.clientscriptmanager.registerstartupscript?view=netframework-4.8

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

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.clientscriptmanager.registerclientscriptblock?view=netframework-4.8

Registers the client script with the Page object.

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.registerclientscriptblock?view=netframework-4.8

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的绑定事件,不会触发

https://stackoverflow.com/questions/8298843/registerclientscriptblock-parameters-usages-in-real-scenarios

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);

https://stackoverflow.com/questions/424375/what-is-the-significance-of-the-type-parameter-in-the-registerclientscriptblock

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)

这两个方法唯一的不同之处在于向“何处”注册脚本块。
         RegisterClientScriptBlock(key, script) 在 form开始处(紧接 <form runat="server"> 标识之后)发送脚本块      
         使用场景:
               一般不使用DOM元素
         RegisterStartupScript(key, script) 在 form结尾处(在 </form> 标识之前)发送脚本块,在document装载完成后会执行,等价于body.onload=f(){}里的内容
         使用场景:
               一般要使用DOM元素,比如:修改dom元素的值等

ClientScriptManager 和 ScriptManager RegisterClientScriptBlock的更多相关文章

  1. ClientScriptManager与ScriptManager向客户端注册脚本的区别

    使用ClientScriptManager向客户端注册脚本 ClientScriptManager在非异步(就是说非AJAX)环境下使用的.如果要在异步环境下注册脚本应该使用ScriptManager ...

  2. ASP.NET后台输出js大全,页面顶部、form表单中前面与后面、和UpdatePanel(ScriptManager、AJAX)输出JS

    Response.Write 与   Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...

  3. 弹出消息对话框ScriptManager

    //直接调用WebMessageBox方法 #region 弹出消息对话框 /// <summary> /// 弹出消息对话框 /// </summary> /// <p ...

  4. RegisterStartupScript和RegisterClientScriptBlock的区别

    1.  //注册到 <form> 尾部 ,HTML元素已加载完毕    this.Page.ClientScript.RegisterStartupScript(this.GetType( ...

  5. ScriptManager的几个属性和方法

    ScriptManager的几个属性和方法   一.EnablePageMethods ScriptManager的EnablePageMethods属性用于设定客户端javascript直接调用服务 ...

  6. C#中js文本提示

    Page.ClientScript 与 ClientScript 的关系 这二者实际上是一个东西,后者只是省略了 Page.都是获取用于管理脚本.注册脚本和向页添加脚本的 ClientScriptMa ...

  7. 【转载】asp.net 后台弹出提示框

    感觉这种最好用: public void showMessage(string str_Message) { ClientScript.RegisterStartupScript(this.GetTy ...

  8. .Net中使用无闪刷新控件时提示框不显示

    今天做提示框的时候一直不显示,让我郁闷好久,晚上吃饭的时候问了同事一下,他给了一个思路, 他说可能是因为由于页面中的无闪刷新导致的结果:百度了一下真找到了解决方法 在页面中存在无闪刷新控件的时候提示框 ...

  9. cPage分页,asp.net自定义分页,url传值分页,支持datalist、gridview、Repeater等

    asp.net分页是最最常用的功能,实现方式也很多,使用不同的控件有不同的分页方式. 下面分享一个我们团队内部使用了多年的一个分页控件cPage,是自己设计编写,没有冗余,简单.快速. cPage,现 ...

随机推荐

  1. js 禁用F12 和右键查看源码

    <script> window.onkeydown = function(e) { if (e.keyCode === 123) { e.preventDefault() } } wind ...

  2. 【学习总结】Markdown 使用-表格及其居中等格式

    参考: Learning-Markdown (Markdown 入门参考)-表格 Markdown 注:主要是github中的使用 要点: 不管是哪种方式,第一行为表头,第二行为分割表头和主体部分,第 ...

  3. 问题:com.alibaba.dubbo.rpc.RpcException: Failed to invoke ......

    个人解决流程: 一看到这个问题,下意识想到了是dubbo远程连接的问题,可能是dubbo本身的问题,于是在虚拟机上另外一台dubbo能正常脸上的服务器上重新尝试,还是报相同的错误,并且在dubbo-a ...

  4. python 出现OSError: [Errno 8] Exec format error的原因

    访问 .py文件的网页的时候会出现 Exec format error的问题, 一般情况下是由于基于Unix(Linux,Mac OS)系统下的问题,办法如下 1 .chmod +x  filenam ...

  5. TIOBE 7月排行:Python 过分炒作,Perl 成受害者?

    与上个月相比,Python 的指数又增加了不少,由 8.530% 上升到 9.260%. 我们还留意到,TIOBE 对这期榜单的标题描述是“Perl is one of the victims of ...

  6. Hibrenate实现根据实体类自动创建表或添加字段

    Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 实现: 在配置hibernate的配置文件中将hbm2ddl.auto设置为update,如:Xml代码&l ...

  7. 标准C语言(13)

    函数指针可以作为形式参数使用,会作为实际参数使用的函数叫回调函数 /* * 回调函数演示 * */ #include <stdio.h> void print_cb(int *p_num) ...

  8. keras多gpu训练

    使用multi_gpu_model即可.观察了一下GPU的利用率,非常的低,大部分时候都是0,估计在相互等待,同步更新模型: 当然了,使用多GPU最明显的好处是可以使用更大的batch size im ...

  9. php socket如何实现长连接

    长连接是什么? 朋友们应该都见过很多在线聊天工具和网页在线聊天的工具.学校内有一种熟悉的功能,如果有人回复你了,网站会马上出现提示,此时你并没有刷新页面:Gmail也有此功能,如果邮箱里收到了新的邮件 ...

  10. STL源码阅读-traits与迭代器

    迭代器模式 提供一种方法,使之能够依序访问容器的各个元素,而又无需暴露容器的内部表述方式 STL设计的中心思想在于将数据容器和算法分离开,容器和算法分开设计,迭代器则是两者之间的胶着剂,一般迭代器的设 ...