ClientScriptManager.RegisterClientScriptBlock Method (Type, String, String) (System.Web.UI)

Switch View :
ScriptFree
.NET Framework Class Library
ClientScriptManager.RegisterClientScriptBlock Method (Type, String, String)

Registers the client script with the Page object using a type, key, and script literal.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic
Public Sub RegisterClientScriptBlock ( _
	type As Type, _
	key As String, _
	script As String _
)
C#
public void RegisterClientScriptBlock(
	Type type,
	string key,
	string script
)
Visual C++
public:
void RegisterClientScriptBlock(
	Type^ type, 
	String^ key, 
	String^ script
)
F#
member RegisterClientScriptBlock : 
        type:Type * 
        key:string * 
        script:string -> unit 

Parameters

type
Type: System.Type
The type of the client script to register.
key
Type: System.String
The key of the client script to register.
script
Type: System.String
The client script literal to register.
Remarks

A client script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

Call the IsClientScriptBlockRegistered method to determine whether a client script with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

In this overload of the RegisterClientScriptBlock method, you must make sure that the script provided in the script parameter is wrapped in a <script> element block.

The RegisterClientScriptBlock method adds a script block to the top of the rendered page. The script blocks are not guaranteed to be output in the order they are registered. If the order of the script blocks is important, use a StringBuilder object to gather the scripts together in a single string, and then register them all in a single client script block.

Examples

The following code example demonstrates the use of the RegisterClientScriptBlock method.

Visual Basic

<%@ Page Language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
        ' Define the name and type of the client script on the page. 
        Dim csName As [String] = "ButtonClickScript"
        Dim csType As Type = Me.[GetType]()

        ' Get a ClientScriptManager reference from the Page class. 
        Dim cs As ClientScriptManager = Page.ClientScript

        ' Check to see if the client script is already registered. 
        If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
            Dim csText As New StringBuilder()
            csText.Append("<script type=""text/javascript""> function DoClick() {")
            csText.Append("Form1.Message.value='Text from client script.'} </")
            csText.Append("script>")
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
        End If
    End Sub
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>


C#

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client script on the page.
    String csName = "ButtonClickScript";
    Type csType = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(csType, csName))
    {
      StringBuilder csText = new StringBuilder();
      csText.Append("<script type=\"text/javascript\"> function DoClick() {");
      csText.Append("Form1.Message.value='Text from client script.'} </");
      csText.Append("script>");
      cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
    }
  }
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Community Content

TheAdventMaster
Modified Example Code
Hello,

I've modified the example code for readability.  I've attempted to keep the code as terse as possible.  I replaced the usage of NOT (Expression) to (Expression = False), which is a hazard to automatically translated code between VB and C#.  Here is the updated code:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!-- -------------------------------------------------------------------------- -->
<!-- A VB script to be executed every time the page is loaded. This includes -->
<!-- first-time page loads and postbacks (for example, from button clicks) -->
<!-- -------------------------------------------------------------------------- -->
<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Dim typeASPX As Type = Me.GetType()
Dim strScriptName1 As String = "PopupScript"
Dim strScriptName2 As String = "ButtonClickScript"

' Retrieve a ClientScriptManager reference from the Page class
Dim csmScriptManager As ClientScriptManager = Page.ClientScript

' Is the startup script NOT already registered?
If (csmScriptManager.IsStartupScriptRegistered(typeASPX, strScriptName1) = False) Then

' Create the script (Note the lack of script tags)
Dim strScriptText1 As String = "alert('Hello World');"

' Register the script (True indicating the script does not contain script tags)
csmScriptManager.RegisterStartupScript(typeASPX, strScriptName1, strScriptText1, True)

End If

' Is the button click script NOT already registers?
If (csmScriptManager.IsClientScriptBlockRegistered(typeASPX, strScriptName2) = False) Then

' Create the button click script
Dim strScriptText2 As New StringBuilder()
strScriptText2.Append("<script type=""text/javascript"">")
strScriptText2.Append(" function btnScriptButton_Click() {")
strScriptText2.Append(" frmMain.txtMessage.value = 'Text from client script.'")
strScriptText2.Append(" } ")
strScriptText2.Append("</" & "script>")

' Register the script (False indicating the script includes its own script tags)
csmScriptManager.RegisterClientScriptBlock(typeASPX, strScriptName2, strScriptText2.ToString(), False)

End If

End Sub

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
<title>ClientScriptManager Example</title>
</head>

<body>
<form id="frmMain" runat="server">

<!-- Text input to retrieve and display -->
<input id="txtMessage" type="text" />

<!-- Interactive button will run the client-side btnScriptButton_Click() procedure -->
<input id="btnScriptButton" type="button" value="Click Me!" onclick="btnScriptButton_Click()" />

</form>
</body>

</html>
I hope this is seen as an improvement,
- Pritchard