RegisterClientScriptBlock parameters usages in real scenarios?

ⅰ亾dé卋堺 提交于 2019-12-06 19:16:30

问题


http://i.stack.imgur.com/dVjHt.jpg

I never understood the real usage of the Control , type,key usages of this class.

In general Ive always used with : this , GetType() , "xx"

but now I truly want to understand .


msdn :

Control : " the control that is registering the client script"

so...? what difference does it makes who registered it ? the script will be in the head of the page...

Type: "the type of the client script block"

type ??? its javascript. why does he want another type from me ?

Key: "a unique indentifier"

That I can understand - for cases which later to remove... but I'd love for some more advanced explanations

Can I have please , a real life scenario in which I TRULY have to play with those params ?


回答1:


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



回答2:


The method System.Web.UI.ScriptManager.RegisterClientScriptBlock calls behind the scenes to the internal method System.Web.UI.ScriptRegistrationManager.RegisterClientScriptBlock that uses the parameter control to make a call to System.Web.UI.ClientScriptManager.RegisterClientScriptBlock by referencing control.Page.ClientScript.RegisterClientScriptBlock.

So, actually when you making a call like this:

MyScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertScript", "alert('hi')", true);

It's identical to calling:

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alertScript", "alert('hi')", true);        

The type parameter comes handy when two different types trying to register scripts with identical string keys.




回答3:


After looking on MSDN I found the method with slightly different parameters, but I think the answers should give you more insight: (http://msdn.microsoft.com/en-us/library/bb350750.aspx) :

Control: Client script blocks that are registered by using this method are sent to the page only when control represents a control that is inside an UpdatePanel control that is being updated.

Type: This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.

And I guess the key just is for making sure that one codeblock is not included twice? Or it gives you a warning if you use the same key again?



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!