Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
Members FilterMembers Filter
Frameworks FilterFrameworks Filter
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ClientScriptManager..::.RegisterClientScriptBlock Method

Registers the client script with the Page object.

  NameDescription
Public methodRegisterClientScriptBlock(Type, String, String)Registers the client script with the Page object using a type, key, and script literal.
Public methodRegisterClientScriptBlock(Type, String, String, Boolean)Registers the client script with the Page object using a type, key, script literal, and Boolean value indicating whether to add script tags.
Top
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
RegisterClientScriptBlock Example      SuperStruct   |   Edit   |   Show History

There has been a lot of confusion as to what the type parameter is with this method. From my questions in the ASP.NET forums, the general consensus is that the parameter should be this.Page.GetType().

Also, the boolean parameter allows you to specify if your string has the script tags or not. Setting it to true means the tags will be automatically added and false means you have to add them in the JavaScript string yourself. For example:

protected void Page_Load(object sender, EventArgs e) 
{ 
  string myScript = "window.alert('hi');"; 
  if (!Page.ClientScript.IsClientScriptBlockRegistered("HelloAlert")) 
  { 
    Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "HelloAlert", myScript, true); 
  } 
} 

This example will generate the JavaScript opening and closing script tags for you. The code generated in IE7 results in this:

<script type="text/javascript"> 
  window.alert('hi'); 
</script> 
Tags What's this?: Add a tag
Flag as ContentBug
What is the type parameter supposed to be      Chris Stransky ... Thomas Lee   |   Edit   |   Show History
There is a lot of community content on this and it isn't all the same. Some people in the 3.0 version of this content say its supposed to be this.GetType() and some people in the 3.5 content say it is supposed to be this.Page.GetType(). Which is correct or does it even matter? Both calls return a Type object so the compiler doesn't care. Some clarification from the source, i.e. Microsoft, seems needed.
The type parameter      Mythran   |   Edit   |   Show History
When designing a web control, the old way of doing this you would specific the key and script. Because of this, authors of web controls may overwrite each other's scripts because they used the same key without realizing it. I believe Microsoft thinks this to be a good approach to prevent this from occuring. In a web control, you can specify Me.GetType(), or this.GetType() in C#, to identify the control in which this script is to be keyed with so multiple key values with the same value can exist on the page, but registered for different control types. In a page, you can just use Me.GetType() if you are registering a script for a page but not a specific control.

This may not be the case, but I believe it to be true (True in Visual Basic).

HTH,
Mythran
Tags What's this?: Add a tag
Flag as ContentBug
The type parameter...      Magnus_Aycox   |   Edit   |   Show History

The confusion about this.GetType() and this.Page.GetType() is partly because it is used from multiple object types.
If you are to refer to the page type and the current context is System.Web.UI.UserControl (i.e. you are currently writing a user control) then you have to write:

this.Page.GetType()

Whereas if you are currently in the System.Web.UI.Page context (i.e. you are currently writing a web page) you would write

this.GetType()


A simple way to end all the confusion would instead be to write

typeof(System.Web.UI.Page)

This will work everywhere and it doesn't confuse anybody.

Cheers.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker