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

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元素的值等

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(770)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-07-09 C#中的异常处理
2015-07-09 How to: Create a Windows Communication Foundation Client
2015-07-09 Sum of Digits / Digital Root
2015-07-09 Multiples of 3 and 5
2015-07-09 Moduli number system
2015-07-09 Case swapping
2014-07-09 zedgraph多个graphpane的处理
点击右上角即可分享
微信分享提示