Export (0) Print
Expand All

ClientScriptManager.RegisterClientScriptBlock Method (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.

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

'Declaration
Public Sub RegisterClientScriptBlock ( _
	type As Type, _
	key As String, _
	script As String, _
	addScriptTags As Boolean _
)
'Usage
Dim instance As ClientScriptManager 
Dim type As Type 
Dim key As String 
Dim script As String 
Dim addScriptTags As Boolean

instance.RegisterClientScriptBlock(type, _
	key, script, addScriptTags)

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.

addScriptTags
Type: System.Boolean

A Boolean value indicating whether to add script tags.

ExceptionCondition
ArgumentNullException

The client script block type is Nothing.

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. This lets you avoid unnecessarily attempting to add the script.

In this overload of the RegisterClientScriptBlock method, you can indicate whether the script provided in the script parameter is wrapped with a <script> element block by using the addScriptTags parameter. Setting addScriptTags to true indicates that script tags will be added automatically.

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. Note that the addScriptTags parameter is set to true so the beginning and closing script tags are not included with the script parameter.

<%@ 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">

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

    ' Define the name and type of the client scripts on the page. 
    Dim csname1 As String = "PopupScript" 
    Dim csname2 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 startup script is already registered. 
    If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then 

      Dim cstext1 As String = "alert('Hello World');"
      cs.RegisterStartupScript(cstype, csname1, cstext1, True)

    End If 

    ' Check to see if the client script is already registered. 
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then 

      Dim cstext2 As New StringBuilder()
            cstext2.Append("<script type=""text/javascript""> function DoClick() {")
      cstext2.Append("Form1.Message.value='Text from client script.'} </")
      cstext2.Append("script>")
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)

    End If 

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft