0 out of 5 rated this helpful - Rate this topic

ClientScriptManager.RegisterStartupScript Method (Type, String, String)

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

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public void RegisterStartupScript(
	Type type,
	string key,
	string script
)

Parameters

type
Type: System.Type
The type of the startup script to register.
key
Type: System.String
The key of the startup script to register.
script
Type: System.String
The startup 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 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 must make sure that the script provided in the script parameter is wrapped with a <script> element block.

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 beginning and closing script tags are included within the script parameter. To have the script tags added based on an additional parameter setting, see the RegisterStartupScript method.


<%@ 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 scripts on the page.
    String csname1 = "PopupScript";
    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))
    {
        StringBuilder cstext1 = new StringBuilder();
        cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
        cstext1.Append("script>");

        cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RegisterStartupScript</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Missing single quotes in the VB example
Line
cstext1.Append("<script type=text/javascript> alert('Hello World!') </")
should be
cstext1.Append("<script type='text/javascript'> alert('Hello World!') </")
Terrible Example
This is a terrible example because it's embedding C# code in the page, rather than having a real code-behind.  The type parameter is not explained, and without a code-behind example cannot be discerned.   Telling commenters to ask questions elsewhere, rather than correcting the bad example, strengthens the popular feeling that Microsoft doesn't care about documentation.
Script Type??
The type parameter is described as "the type of script that will be registered" but is usually exemplified with this.getType() which is the Page type of object???

What is this parameter really for?

[tfl] Thank you for your feedback. For these kinds of questions, please try the Microsoft forums:  http://social.answers.microsoft.com/Forums/en