ClientScriptManager.RegisterClientScriptInclude Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Zaregistruje klientský skript, který je součástí objektu Page .
Přetížení
| Name | Description |
|---|---|
| RegisterClientScriptInclude(String, String) |
Zaregistruje klientský skript s Page objektem pomocí klíče a adresy URL, což 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, což 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
Součástí klientského skriptu je jedinečný identifikátor jeho klíče a jeho typu. 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ž zaregistrován, nevytvoří duplikát skriptu.
IsClientScriptIncludeRegistered Voláním metody určete, jestli je klientský skript součástí daného klíče a páru typů již zaregistrovaný, a vyhněte se zbytečnému pokusu o přidání skriptu.
Note
K překladu adresy URL klienta použijte metodu ResolveClientUrl . Tato metoda používá kontext adresy URL, na které je volána k překladu cesty.
Toto přetížení RegisterClientScriptInclude metody volá přetížení, které přebírá key, URLa a type parametr.
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.
Příklady
Následující příklad kódu ukazuje použití RegisterClientScriptInclude metody. Všimněte si, že pokud byla odstraně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 javascriptový soubor 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, stejně jako type parametr pro určení identifikace klientského skriptu zahrnout. 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.
Note
K překladu adresy URL klienta použijte metodu ResolveClientUrl . Tato metoda používá kontext adresy URL, na které je volána k překladu cesty.
Tato metoda přidá blok skriptu v horní části vykreslené stránky.