Partage via


ClientScriptManager.IsClientScriptIncludeRegistered Méthode

Définition

Détermine si le script client Include est inscrit avec l'objet Page.

Surcharges

IsClientScriptIncludeRegistered(String)

Détermine si le script client Include est inscrit avec l'objet Page à l'aide de la clé spécifiée.

IsClientScriptIncludeRegistered(Type, String)

Détermine si le script client Include est inscrit avec l'objet Page à l'aide d'une clé et d'un type.

IsClientScriptIncludeRegistered(String)

Détermine si le script client Include est inscrit avec l'objet Page à l'aide de la clé spécifiée.

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

Paramètres

key
String

Clé du script client Include à rechercher.

Retours

Boolean

true si le script client Include est enregistré ; sinon, false.

Remarques

Appelez cette méthode avant d’appeler la méthode pour éviter d’inscrire RegisterClientScriptInclude des scripts en double. Cela est particulièrement important si le script nécessite une grande quantité de ressources serveur à créer.

Un script client include est identifié de manière unique par sa clé et son type. Les scripts avec la même clé et le même type sont considérés comme des doublons.

Cette surcharge de la IsStartupScriptRegistered méthode appelle la surcharge qui prend à la fois un key paramètre avec type le type défini en tant qu’objet Page .

Voir aussi

S’applique à

IsClientScriptIncludeRegistered(Type, String)

Détermine si le script client Include est inscrit avec l'objet Page à l'aide d'une clé et d'un type.

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

Paramètres

type
Type

Type du script client Include à rechercher.

key
String

Clé du script client Include à rechercher.

Retours

Boolean

true si le script client Include est enregistré ; sinon, false.

Exceptions

Le type du script client Include est null.

Exemples

L’exemple de code suivant illustre l’utilisation de la IsClientScriptIncludeRegistered méthode. Notez que, si la logique de vérification du script client existant a été supprimée, il n’y aurait pas deux scripts clients en double dans le code source HTML de la page rendue, car la méthode vérifie les RegisterClientScriptInclude doublons. L’avantage de la vérification est de réduire les calculs inutiles.

<%@ 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, type and url of the client script on the page.
        String csname = "ButtonClickScript";
        String csurl = "~/script_include.js";
        Type cstype = this.GetType();

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

        // Check to see if the include script exists already.
        if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
        {
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
        }

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1" runat="server">
     <div>
        <input type="text"
               id="Message"/> 
        <input type="button" 
               value="ClickMe"
               onclick="DoClick()"/>
     </div>
     </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, type and url of the client script on the page.
        Dim csname As String = "ButtonClickScript"
        Dim csurl As String = "~/script_include.js"
        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 include script is already registered.
        If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
      
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
      
        End If
    
    End Sub

</script>

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

Cet exemple nécessite un fichier JavaScript nommé Script_include.js, avec le contenu suivant :

function DoClick() {Form1.Message.value='Text from include script.'}  

Remarques

Appelez cette méthode avant d’appeler la RegisterClientScriptInclude méthode pour éviter d’inscrire le script client en double. Cela est particulièrement important si le script nécessite une grande quantité de ressources serveur à créer.

Un script client include est identifié de manière unique par sa clé et son type. Les scripts avec la même clé et le même type sont considérés comme des doublons. Vous spécifiez le type en fonction de l’objet qui accède à la ressource. Par exemple, lorsque vous utilisez une instance de page pour accéder à la ressource, vous spécifiez le Page type.

Voir aussi

S’applique à