Udostępnij za pośrednictwem


ClientScriptManager.RegisterClientScriptInclude Metoda

Definicja

Rejestruje skrypt klienta dołączany do Page obiektu .

Przeciążenia

RegisterClientScriptInclude(String, String)

Rejestruje skrypt klienta w Page obiekcie przy użyciu klucza i adresu URL, który umożliwia wywoływanie skryptu z klienta.

RegisterClientScriptInclude(Type, String, String)

Rejestruje skrypt klienta dołączany do Page obiektu przy użyciu typu, klucza i adresu URL.

RegisterClientScriptInclude(String, String)

Rejestruje skrypt klienta w Page obiekcie przy użyciu klucza i adresu URL, który umożliwia wywoływanie skryptu z klienta.

public:
 void RegisterClientScriptInclude(System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude (string key, string url);
member this.RegisterClientScriptInclude : string * string -> unit
Public Sub RegisterClientScriptInclude (key As String, url As String)

Parametry

key
String

Klucz skryptu klienta obejmuje rejestrację.

url
String

Adres URL skryptu klienta należy uwzględnić w celu zarejestrowania.

Przykłady

Aby uzyskać powiązane informacje, w tym składnię, użycie i przykład, zobacz RegisterClientScriptInclude.

Uwagi

Dołączany skrypt klienta jest jednoznacznie identyfikowany przez jego klucz i jego typ. Skrypty z tym samym kluczem i typem są traktowane jako duplikaty. Na stronie można zarejestrować tylko jeden skrypt z danym typem i parą kluczy. Próba zarejestrowania skryptu, który jest już zarejestrowany, nie powoduje utworzenia duplikatu skryptu.

Wywołaj metodę , IsClientScriptIncludeRegistered aby określić, czy skrypt klienta dołączany do danego klucza i pary typów jest już zarejestrowany i unikaj niepotrzebnej próby dodania skryptu.

Uwaga

Aby rozpoznać adres URL klienta, użyj ResolveClientUrl metody . Ta metoda używa kontekstu adresu URL, na którym jest wywoływana, aby rozpoznać ścieżkę.

To przeciążenie metody wywołuje przeciążenieRegisterClientScriptInclude, które przyjmuje keyparametr , i URL``type .

Metoda dodaje blok skryptu w górnej części renderowanej strony.

Zobacz też

Dotyczy

RegisterClientScriptInclude(Type, String, String)

Rejestruje skrypt klienta dołączany do Page obiektu przy użyciu typu, klucza i adresu URL.

public:
 void RegisterClientScriptInclude(Type ^ type, System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude (Type type, string key, string url);
member this.RegisterClientScriptInclude : Type * string * string -> unit
Public Sub RegisterClientScriptInclude (type As Type, key As String, url As String)

Parametry

type
Type

Typ skryptu klienta należy uwzględnić do zarejestrowania.

key
String

Klucz skryptu klienta obejmuje rejestrację.

url
String

Adres URL skryptu klienta należy uwzględnić w celu zarejestrowania.

Wyjątki

Typ dołączania skryptu klienta to null.

Adres URL to null.

-lub- Adres URL jest pusty.

Przykłady

W poniższym przykładzie kodu pokazano użycie RegisterClientScriptInclude metody . Należy pamiętać, że jeśli logika do sprawdzenia, czy istniejący skrypt klienta został usunięty, nadal nie będzie zduplikowanych skryptów klienta na renderowanej stronie, ponieważ RegisterClientScriptInclude metoda sprawdza duplikaty. Zaletą sprawdzania jest zmniejszenie niepotrzebnych obliczeń.

<%@ 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>

W tym przykładzie wymagany jest plik JavaScript o nazwie Script_include.js o następującej zawartości:

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

Uwagi

To przeciążenie RegisterClientScriptInclude metody przyjmuje parametry klucza i adresu URL w celu zidentyfikowania skryptu, a także type parametr określający identyfikację skryptu klienta. Typ określa się na podstawie obiektu, który będzie uzyskiwać dostęp do zasobu. Na przykład w przypadku używania Page wystąpienia do uzyskiwania dostępu do zasobu należy określić Page typ.

Uwaga

Aby rozpoznać adres URL klienta, użyj ResolveClientUrl metody . Ta metoda używa kontekstu adresu URL, na którym jest wywoływana, aby rozpoznać ścieżkę.

Ta metoda dodaje blok skryptu w górnej części renderowanej strony.

Zobacz też

Dotyczy