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)

Public Sub RegisterClientScriptBlock (
	type As Type,
	key As String,
	script As String


Type: System.Type

The type of the client script to register.

Type: System.String

The key of the client script to register.

Type: System.String

The client script literal to register.

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.

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

<%@ Page Language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<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.'} </")
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
        End If
    End Sub
<html  >
    <title>RegisterClientScriptBlock Example</title>
     <form id="Form1"
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />

.NET Framework
Available since 2.0
Return to top