ClientScriptManager GetWebResourceUrl 方法

ClientScriptManager..::.GetWebResourceUrl 方法

更新:2007 年 11 月

来源:http://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx

http://msdn.microsoft.com/zh-cn/library/system.web.ui.page.clientscript(VS.80).aspx

 

获取对程序集内资源的 URL 引用。

命名空间:  System.Web.UI
程序集:  System.Web(在 System.Web.dll 中)

Visual Basic(声明)
Public Function GetWebResourceUrl ( _
type As Type, _
resourceName As String _
) As String
Visual Basic (用法)
Dim instance As ClientScriptManager
Dim type As Type
Dim resourceName As String
Dim returnValue As String
returnValue = instance.GetWebResourceUrl(type, _
resourceName)
C#
public string GetWebResourceUrl(
Type type,
string resourceName
)
Visual C++
public:
String^ GetWebResourceUrl(
Type^ type,
String^ resourceName
)
J#
public String GetWebResourceUrl(
Type type,
String resourceName
)
JScript
public function GetWebResourceUrl(
type : Type,
resourceName : String
) : String

参数

type
类型:System..::.Type

资源的类型。

resourceName
类型:System..::.String

程序集内资源的完全限定名称。

返回值

类型:System..::.String

对资源的 URL 引用。

异常 条件
ArgumentNullException

Web 资源类型为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

ArgumentNullException

Web 资源名称为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

- 或 -

Web 资源名称长度为零。

GetWebResourceUrl 方法返回对程序集内嵌入的资源的 URL 引用。返回的引用不是 URL 编码的。资源可以是脚本文件、图像或任何静态文件。根据将要访问资源的对象来指定类型。

使用该页面注册的 Web 资源由其类型和名称唯一标识。只有一个具有给定的类型和名称对的资源可向该页面注册。试图注册一个已经注册的资源不会创建已注册资源的副本。

GetWebResourceUrl 方法与 RegisterClientScriptResource 方法结合使用以访问嵌入程序集中的资源。有关在应用程序中使用资源的更多信息,请参见 ASP.NET 网页资源概述

下面的代码示例演示 GetWebResourceUrl 方法的用法。此示例中的 type 参数设置为包含此资源的程序集中的类的类型。用包含默认命名空间的资源的完全限定路径指定 resourceName 参数。

Visual Basic
 
<%@ Page Language="VB" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>
<!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 resource name and type.
Dim rsname As String = "Samples.AspNet.VB.Controls.script_include.js"
Dim rstype As Type = GetType(ClientScriptResourceLabel)
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Write out the web resource url.
ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname)
' Register the client resource with the page.
cs.RegisterClientScriptResource(rstype, rsname)
End Sub
</script>
<html  >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form    id="Form1"
runat="server">
The web resource path is
<span  id="ResourcePath"
runat="server"/>.
<br />
<br />
<input type="text"
id="Message" />
<input type="button"
onclick="DoClick()"
value="ClientClick" />
</form>
</body>
</html>
C#
 
<%@ Page Language="C#"%>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>
<!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 resource name and type.
String rsname = "Samples.AspNet.CS.Controls.script_include.js";
Type rstype = typeof(ClientScriptResourceLabel);
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Write out the web resource url.
ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname);
// Register the client resource with the page.
cs.RegisterClientScriptResource(rstype, rsname);
}
</script>
<html  >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form    id="Form1"
runat="server">
The web resource path is
<span  id="ResourcePath"
runat="server"/>.
<br />
<br />
<input type="text"
id="Message" />
<input type="button"
onclick="DoClick()"
value="ClientClick" />
</form>
</body>
</html>

下面的代码示例演示如何以编程方式应用 WebResourceAttribute 元数据属性以标记要服务的资源程序集。用设置为 Samples.AspNet.CS.ControlsSamples.AspNet.VB.Controls(具体取决于所使用的语言)的默认命名空间编译类库中的以下类。

Visual Basic
 
Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Security.Permissions
<Assembly: WebResource("Samples.AspNet.VB.Controls.script_include.js", "application/x-javascript")>
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class ClientScriptResourceLabel
' Class code goes here.
End Class
End Namespace
C#
 
using System;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
[assembly: WebResource("Samples.AspNet.CS.Controls.script_include.js", "application/x-javascript")]
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ClientScriptResourceLabel
{
// Class code goes here.
}
}

此示例需要一个名为 Script_include.js 的 JavaScript 文件。该 .js 文件是程序集内包含 ClientScriptResourceLabel 对象的嵌入资源。如果要使用 Visual Studio,请在选择脚本文件时在类库项目的“属性”窗口中将“生成操作”设置为“嵌入资源”。如果要在命令行编译库,请使用 /resource 开关嵌入资源。

 

 

Page.ClientScript 属性

注意:此属性在 .NET Framework 2.0 版中是新增的。

获取用于管理脚本、注册脚本和向页添加脚本的 ClientScriptManager 对象。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public ReadOnly Property ClientScript As ClientScriptManager
Visual Basic(用法)
Dim instance As Page
Dim value As ClientScriptManager
value = instance.ClientScript
C#
public ClientScriptManager ClientScript { get; }
C++
public:
property ClientScriptManager^ ClientScript {
ClientScriptManager^ get ();
}
J#
/** @property */
public ClientScriptManager get_ClientScript ()
JScript
public function get ClientScript () : ClientScriptManager

属性值

一个 ClientScriptManager 对象。
posted @ 2009-01-19 10:35  云飞扬IT  阅读(1135)  评论(3编辑  收藏  举报