This documentation is archived and is not being maintained.

Page.IsStartupScriptRegistered Method

Determines if the client startup script is registered with the Page object.

[Visual Basic]
Public Function IsStartupScriptRegistered( _
   ByVal key As String _
) As Boolean
[C#]
public bool IsStartupScriptRegistered(
 string key
);
[C++]
public: bool IsStartupScriptRegistered(
 String* key
);
[JScript]
public function IsStartupScriptRegistered(
   key : String
) : Boolean;

Parameters

key
The string key of the startup script to search for.

Return Value

true if the startup script is registered; otherwise, false.

Remarks

Call this method before calling Page.RegisterStartupScript to avoid unnecessarily assembling the client-side script. This is particularly important if the script requires a large amount of server resources to create.

Example

[Visual Basic, C#] The following example demonstrates the use of the RegisterStartupScript method in conjunction with IsStartupScriptRegistered method. If the ECMAScript written in the code-declaration block has not already been registered, as tested by IsStartupScriptRegistered, the a RegisterStartupBlock call is made.

[Visual Basic] 
<html>
  <head>
     <script language="VB" runat="server">
     public Sub Page_Load(Sender As Object,e As EventArgs)     
       ' Form the script to be registered at client side.
         Dim scriptString As String  = "<script language=JavaScript> function DoClick() {"
             scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}"
         scriptString += "function Page_Load(){ showMessage1.innerHTML="
         scriptString += "'<h4>RegisterStartupScript Example</h4>'}<"
         scriptString += "/"
         scriptString += "script>"

             If(Not Me.IsStartupScriptRegistered("Startup")) Then
        Me.RegisterStartupScript("Startup", scriptString)
             End If
    End Sub
        </script>
  </head>
  <body topmargin="20" leftmargin="10" onload="Page_Load()">
     <form id="myForm" runat="server">
        <span id="showMessage1"></span>
        <br>
        <input type="button" value="ClickMe" onclick="DoClick()">
        <br>
        <span id="showMessage2"></span>
     </form>
  </body>
</html>

[C#] 
<html>
  <head>
    <script language="C#" runat="server">
    public void Page_Load(Object sender, EventArgs e) {
       // Form the script to be registered at client side.
       String scriptString = "<script language=JavaScript> function DoClick() {";
       scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}";
       scriptString += "function Page_Load(){ showMessage1.innerHTML=";
          scriptString += "'<h4>RegisterStartupScript Example</h4>'}<";
       scriptString += "/";
       scriptString += "script>";
        
           if(!this.IsStartupScriptRegistered("Startup"))
         this.RegisterStartupScript("Startup", scriptString);
     }


     </script>
  </head>
  <body topmargin="20" leftmargin="10" onload="Page_Load()">
     <form id="myForm" runat="server">
        <span id="showMessage1"></span>
        <br>
        <input type="button" value="ClickMe" onclick="DoClick()">
        <br>
        <span id="showMessage2"></span>
     </form>
  </body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Page Class | Page Members | System.Web.UI Namespace | RegisterStartupScript

Show: