ScriptReferenceEventArgs.Script 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取包含脚本库的 ScriptReference 对象。
public:
property System::Web::UI::ScriptReference ^ Script { System::Web::UI::ScriptReference ^ get(); };
public System.Web.UI.ScriptReference Script { get; }
member this.Script : System.Web.UI.ScriptReference
Public ReadOnly Property Script As ScriptReference
属性值
一个 ScriptReference 对象,包含在网页上引用的客户端脚本。
示例
下面的代码示例演示引用名为 CustomScript.js 的脚本文件的 ASP.NET 网页。 当 Web 应用程序托管在域上 www.contoso.com
时,[ScriptReference.Path]Path 属性设置为特定于该域的值。 事件的事件处理程序 ResolveScriptReference 检查主机 URL,并更改 [ScriptReference.Path]Path 属性的值(如有必要)。 通过 ScriptReferenceScript 属性检索 对象。
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void ScriptManager1_ResolveScriptReference(object sender, ScriptReferenceEventArgs e)
{
if (e.Script.Path.Contains("CustomScript"))
{
if (HttpContext.Current.Request.Url.Host.ToLower() == "www.contoso.com")
{
e.Script.Path = "http://www.contoso.com/ScriptRepository/CustomScript.js";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Script Reference Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager OnResolveScriptReference="ScriptManager1_ResolveScriptReference" ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/scripts/CustomScript.js" />
</Scripts>
</asp:ScriptManager>
</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 ScriptManager1_ResolveScriptReference(ByVal sender As Object, ByVal e As ScriptReferenceEventArgs)
If (e.Script.Path.Contains("CustomScript")) Then
If (HttpContext.Current.Request.Url.Host.ToLower() = "www.contoso.com") Then
e.Script.Path = "http://www.contoso.com/ScriptRepository/CustomScript.js"
End If
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Script Reference Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager OnResolveScriptReference="ScriptManager1_ResolveScriptReference" ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/scripts/CustomScript.js" />
</Scripts>
</asp:ScriptManager>
</div>
</form>
</body>
</html>
注解
引发ScriptReference事件时ResolveScriptReference, Script 属性中的 对象将添加到 Scripts 属性中。