ClientScriptManager.RegisterClientScriptInclude 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將用戶端腳本包含在 Page 物件中註冊。
多載
| 名稱 | Description |
|---|---|
| RegisterClientScriptInclude(String, String) |
透過金鑰和網址將用戶端腳本註冊為 Page 物件,讓用戶端能夠呼叫該腳本。 |
| RegisterClientScriptInclude(Type, String, String) |
透過型別、鍵和網址,將客戶端腳本包含的 Page 物件註冊。 |
RegisterClientScriptInclude(String, String)
透過金鑰和網址將用戶端腳本註冊為 Page 物件,讓用戶端能夠呼叫該腳本。
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)
參數
- key
- String
客戶端腳本的鍵包括註冊。
- url
- String
客戶端腳本的網址包含註冊。
範例
相關資訊,包括語法、用法及範例,請參見 RegisterClientScriptInclude。
備註
用戶端腳本包含以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。 該頁面只能註冊一個具有特定類型與金鑰對的腳本。 嘗試註冊已註冊的腳本不會產生該腳本的重複。
呼叫 IsClientScriptIncludeRegistered 該方法來判斷客戶端腳本包含特定鍵與型別的包含是否已註冊,並避免不必要的嘗試新增該腳本。
Note
要解析客戶端網址,請使用以下 ResolveClientUrl 方法。 此方法利用被呼叫的 URL 上下文來解析路徑。
此方法的超載 RegisterClientScriptInclude 呼叫了 ,該過載會取 、 keya URL、 及參數 type 。
此方法會在渲染頁面頂端新增一個腳本區塊。
另請參閱
適用於
RegisterClientScriptInclude(Type, String, String)
透過型別、鍵和網址,將客戶端腳本包含的 Page 物件註冊。
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)
參數
- type
- Type
客戶端腳本的類型包括註冊。
- key
- String
客戶端腳本的鍵包括註冊。
- url
- String
客戶端腳本的網址包含註冊。
例外狀況
用戶端腳本的 include 型別為 null。
範例
以下程式碼範例示範了此 RegisterClientScriptInclude 方法的使用。 請注意,即使移除檢查現有客戶端腳本包含的邏輯,渲染頁面中仍不會有重複的客戶端腳本,因為該 RegisterClientScriptInclude 方法會檢查重複。 檢查的好處是減少不必要的計算。
<%@ 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>
此範例需要一個名為 Script_include.js 的 JavaScript 檔案,內容如下:
function DoClick() {Form1.Message.value='Text from include script.'}
備註
此方法過載 RegisterClientScriptInclude 會使用 鍵 與 網址 參數來識別腳本,並 type 指定客戶端腳本的識別參數。 你可以根據將要存取資源的物件來指定類型。 例如,當使用 Page 實例存取資源時,你指定 Page 了類型。
Note
要解析客戶端網址,請使用以下 ResolveClientUrl 方法。 此方法利用被呼叫的 URL 上下文來解析路徑。
此方法會在渲染頁面頂端新增一個腳本區塊。