ClientScriptManager.RegisterStartupScript Method (Type, String, String, Boolean)


Registers the startup script with the Page object using a type, a key, a script literal, and a Boolean value indicating whether to add script tags.

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

public void RegisterStartupScript(
	Type type,
	string key,
	string script,
	bool addScriptTags


Type: System.Type

The type of the startup script to register.

Type: System.String

The key of the startup script to register.

Type: System.String

The startup script literal to register.

Type: System.Boolean

A Boolean value indicating whether to add script tags.

Exception Condition

type is null.

A startup 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 IsStartupScriptRegistered method to determine whether a startup script with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

In this overload of the RegisterStartupScript 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 script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised. 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 RegisterStartupScript method. Note that the addScriptTags parameter is set to false so the beginning and closing script tags are included with the script parameter.

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();

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

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
<html xmlns="" >
    <title>ClientScriptManager 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