13

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

enter image description here

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 ?

4

3 回答 3

3

最重要的部分是控制要注册脚本的 html 标记中的哪个控件,例如,如果您有用户控件并且只想为此运行脚本,请使用此行

ScriptManager.RegisterStartupScript(this, this.GetType(), "alertscript", "document.getElementById('userControl_h1TAG')", true); 

但是当您想将块和脚本注册到该页面的所有部分时,请在用户控件的 CS 代码中使用这一行:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "alertscript", "document.getElementById('page_h1TAG')", true);
于 2011-11-29T11:10:11.787 回答
1

该方法System.Web.UI.ScriptManager.RegisterClientScriptBlock在后台调用内部方法,该方法System.Web.UI.ScriptRegistrationManager.RegisterClientScriptBlock使用参数控件System.Web.UI.ClientScriptManager.RegisterClientScriptBlock通过引用进行调用control.Page.ClientScript.RegisterClientScriptBlock

所以,实际上当你这样打电话时:

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

这与调用相同:

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

当两种不同的类型尝试使用相同的字符串键注册脚本时, type参数会派上用场。

于 2011-11-29T09:48:52.180 回答
0

在查看 MSDN 后,我发现该方法的参数略有不同,但我认为答案应该能让您更深入地了解:(http://msdn.microsoft.com/en-us/library/bb350750.aspx):

控件:仅当控件表示正在更新的 UpdatePanel 控件内部的控件时,才将使用此方法注册的客户端脚本块发送到页面。

类型:通常使用 typeof 运算符 (C#) 或 GetType 运算符 (Visual Basic) 指定此参数,以检索正在注册脚本的控件的类型

而且我想关键是确保一个代码块不包含两次?或者如果您再次使用相同的密钥,它会给您一个警告?

于 2011-11-28T16:45:50.463 回答