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)
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.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的处理