ClientScriptManager.RegisterClientScriptInclude Metoda

Definice

Zaregistruje klientský skript, který je součástí objektu Page .

Přetížení

RegisterClientScriptInclude(String, String)

Zaregistruje klientský skript s Page objektem pomocí klíče a adresy URL, která umožňuje volání skriptu z klienta.

RegisterClientScriptInclude(Type, String, String)

Zaregistruje klientský skript, který je součástí objektu Page , pomocí typu, klíče a adresy URL.

RegisterClientScriptInclude(String, String)

Zaregistruje klientský skript s Page objektem pomocí klíče a adresy URL, která umožňuje volání skriptu 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

Klíč klientského skriptu zahrnuje registraci.

url
String

Adresa URL klientského skriptu zahrnuje registraci.

Příklady

Související informace, včetně syntaxe, použití a příkladu, najdete v tématu RegisterClientScriptInclude.

Poznámky

Mezi klientské skripty patří jedinečný klíč a jeho typ. Skripty se stejným klíčem a typem se považují za duplicitní. Na stránce lze zaregistrovat pouze jeden skript s daným typem a párem klíčů. Pokus o registraci skriptu, který je již zaregistrovaný, nevytváří duplikát skriptu.

IsClientScriptIncludeRegistered Voláním metody určete, jestli je klientský skript zahrnutý s daným klíčem a párem typů již zaregistrovaný, a vyhněte se zbytečnému pokusu o přidání skriptu.

Poznámka

K vyřešení adresy URL klienta použijte metodu ResolveClientUrl . Tato metoda používá kontext adresy URL, na které se volá k překladu cesty.

Toto přetížení RegisterClientScriptInclude metody volá přetížení, které přebírá key, URLa parametr type .

Metoda přidá blok skriptu v horní části vykreslené stránky.

Viz také

Platí pro

RegisterClientScriptInclude(Type, String, String)

Zaregistruje klientský skript, který je součástí objektu Page , pomocí typu, klíče a adresy 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 klientského skriptu zahrnuje registraci.

key
String

Klíč klientského skriptu zahrnuje registraci.

url
String

Adresa URL klientského skriptu zahrnuje registraci.

Výjimky

Typ zahrnutí klientského skriptu je null.

Adresa URL je null.

-nebo- Adresa URL je prázdná.

Příklady

Následující příklad kódu ukazuje použití RegisterClientScriptInclude metody. Všimněte si, že pokud byla odebrána logika pro kontrolu existujícího klientského skriptu, na vykreslené stránce by stále nebyly duplicitní klientské skripty, protože RegisterClientScriptInclude metoda kontroluje duplicity. Výhodou kontroly je snížení nepotřebných výpočtů.

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

Tento příklad vyžaduje soubor JavaScriptu s názvem Script_include.js s následujícím obsahem:

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

Poznámky

Toto přetížení RegisterClientScriptInclude metody přebírá parametry klíče a adresy URL k identifikaci skriptu a také type parametr pro určení identifikace klientského skriptu. Typ zadáte na základě objektu, který bude přistupovat k prostředku. Například při použití Page instance pro přístup k prostředku zadáte Page typ.

Poznámka

K vyřešení adresy URL klienta použijte metodu ResolveClientUrl . Tato metoda používá kontext adresy URL, na které se volá k překladu cesty.

Tato metoda přidá blok skriptu v horní části vykreslené stránky.

Viz také

Platí pro