ClientScriptManager.RegisterClientScriptInclude 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向 Page 对象注册客户端脚本包含。
重载
RegisterClientScriptInclude(String, String) |
使用让客户端能够调用脚本的键和 URL 向 Page 对象注册客户端脚本。 |
RegisterClientScriptInclude(Type, String, String) |
使用类型、键和 URL 向 Page 对象注册客户端脚本包含。 |
RegisterClientScriptInclude(String, String)
使用让客户端能够调用脚本的键和 URL 向 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
要注册的客户端脚本包含的 URL。
示例
有关相关信息,包括语法、用法和示例,请参阅 RegisterClientScriptInclude。
注解
客户端脚本包含由其密钥及其类型唯一标识。 具有相同键和类型的脚本被视为重复项。 只有一个具有给定类型和密钥配对的脚本才能注册到页面。 尝试注册已注册的脚本不会创建脚本的副本。
调用该方法IsClientScriptIncludeRegistered以确定客户端脚本是否已注册给定密钥和类型配对,并避免不必要的尝试添加脚本。
备注
若要解析客户端 URL,请使用 ResolveClientUrl 该方法。 此方法使用调用 URL 的上下文来解析路径。
此方法的 RegisterClientScriptInclude 此重载调用采用 、 key
a URL
和参数的 type
重载。
该方法在呈现的页面顶部添加脚本块。
另请参阅
适用于
RegisterClientScriptInclude(Type, String, String)
使用类型、键和 URL 向 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
要注册的客户端脚本包含的 URL。
例外
客户端脚本包含类型为 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 重载采用 键 和 URL 参数来标识脚本,以及指定 type
客户端脚本包括的标识的参数。 根据将访问资源的对象指定类型。 例如,使用 Page
实例访问资源时,请指定 Page
类型。
备注
若要解析客户端 URL,请使用 ResolveClientUrl 该方法。 此方法使用调用 URL 的上下文来解析路径。
此方法在呈现的页面顶部添加脚本块。