Share via


ClientScriptManager.IsStartupScriptRegistered Método

Definición

Determina si el script de inicio se encuentra registrado con el objeto Page.

Sobrecargas

IsStartupScriptRegistered(Type, String)

Determina si el script de inicio se encuentra registrado con el objeto Page utilizando la clave y el tipo especificados.

IsStartupScriptRegistered(String)

Determina si el script de inicio se encuentra registrado con el objeto Page utilizando la clave especificada.

IsStartupScriptRegistered(Type, String)

Determina si el script de inicio se encuentra registrado con el objeto Page utilizando la clave y el tipo especificados.

public:
 bool IsStartupScriptRegistered(Type ^ type, System::String ^ key);
public bool IsStartupScriptRegistered (Type type, string key);
member this.IsStartupScriptRegistered : Type * string -> bool
Public Function IsStartupScriptRegistered (type As Type, key As String) As Boolean

Parámetros

type
Type

Tipo del script de inicio que se va a buscar.

key
String

Clave del script de inicio que se va a buscar.

Devoluciones

Boolean

Es true si el script de inicio está registrado; en caso contrario, es false.

Excepciones

El tipo de script es null.

Ejemplos

En el ejemplo de código siguiente se muestra el uso del IsStartupScriptRegistered método . Tenga en cuenta que, si se quitó la lógica para comprobar el bloque de script de inicio existente, no habría dos scripts de inicio duplicados en el código fuente HTML de la página representada porque el RegisterStartupScript método comprueba si hay duplicados. La ventaja de comprobar es reducir el cálculo innecesario.

<%@ 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";
    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.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
  }
</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>
<%@ 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>

Comentarios

Llame a este método antes de llamar al RegisterStartupScript método para evitar el registro de scripts duplicados. Esto es especialmente importante si el script requiere una gran cantidad de recursos de servidor para crear.

Un script de inicio de cliente se identifica de forma única por su clave y su tipo. Los scripts con la misma clave y tipo se consideran duplicados.

Consulte también

Se aplica a

IsStartupScriptRegistered(String)

Determina si el script de inicio se encuentra registrado con el objeto Page utilizando la clave especificada.

public:
 bool IsStartupScriptRegistered(System::String ^ key);
public bool IsStartupScriptRegistered (string key);
member this.IsStartupScriptRegistered : string -> bool
Public Function IsStartupScriptRegistered (key As String) As Boolean

Parámetros

key
String

Clave del script de inicio que se va a buscar.

Devoluciones

Boolean

Es true si el script de inicio está registrado; en caso contrario, es false.

Comentarios

Llame a este método antes de llamar al RegisterStartupScript método para evitar el registro de scripts duplicados. Esto es especialmente importante si el script requiere una gran cantidad de recursos de servidor para crear.

Un script de inicio se identifica de forma única por su clave y su tipo. Los scripts con la misma clave y tipo se consideran duplicados.

Esta sobrecarga del IsStartupScriptRegistered método llama a la sobrecarga que toma una cadena key y un type parámetro con el tipo establecido como un Page objeto

Consulte también

Se aplica a