ClientScriptManager.RegisterClientScriptResource(Type, String) Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Az ügyfélszkript-erőforrást egy típus és egy erőforrásnév használatával regisztrálja az Page objektummal.
public:
void RegisterClientScriptResource(Type ^ type, System::String ^ resourceName);
public void RegisterClientScriptResource(Type type, string resourceName);
member this.RegisterClientScriptResource : Type * string -> unit
Public Sub RegisterClientScriptResource (type As Type, resourceName As String)
Paraméterek
- type
- Type
A regisztrálandó ügyfélszkript-erőforrás típusa.
- resourceName
- String
A regisztrálandó ügyfélszkript-erőforrás neve.
Kivételek
Az ügyfél erőforrástípusa a következő null: .
-vagy-
Az ügyfélerőforrás neve .null
-vagy-
Az ügyfélerőforrás neve nulla hosszúságú.
Példák
Az alábbi példakód a metódus használatát RegisterClientScriptResource mutatja be.
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
Az alábbi példakód bemutatja, hogyan alkalmazhatja programozott módon a WebResourceAttribute metaadat-attribútumot a kiszolgálni kívánt erőforrások szerelvényének megjelölésére.
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.
}
}
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
Ehhez a példához egy JavaScript-fájlra Script_include.jsvan szükség, amely a következő tartalommal rendelkezik:
function DoClick() {Form1.Message.value='Text from resource script.'}
Állítsa össze a Script_include.js fájlt erőforrásként az Samples.AspNet.CS.Controls osztályt tartalmazó ClientScriptResourceLabel szerelvényben.
Megjegyzések
Ez RegisterClientScriptResource a módszer akkor használatos, ha a webresource.axd HTTP-kezelőn keresztül hozzáfér a szerelvényekből származó lefordított erőforrásokhoz. A RegisterClientScriptResource metódus regisztrálja a szkriptet az objektummal, és megakadályozza a Page duplikált szkripteket. Ez a metódus az erőforrás URL-címének tartalmát elemblokkokkal <script> burkolja.