[ASP.NET]將 Script 和 Image 封裝在 dll 內
三月 28, 2009
本文介紹:如何將靜態檔案(如:.html files, .css files, image files, and script files)封裝在 dll 內及如何使用。(.NET 2.0 以上適合)
優點:
- 可避免靜態檔案放錯位置而造成錯誤。
- 許多第三方商用元件也是使用此法。
類別庫(DLL)
新增專案:Visual C#\Windows\類別庫
- 加入參考 System.Web
為了要繼承 System.Web.UI.WebControls.WebControl 及相關 functions 。 - 加入所需的靜態檔案,並將其建置動作設為「內嵌資源」
(如:下圖的 icon_Left_arrow.gif, icon_Right_arrow.gif , JScript1.js , JScript2.js)
- 修改 AssemblyInfo.cs
為每一靜態檔案登記
[assembly: System.Web.UI.WebResource("JsClassLibrary1.JScript1.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("JsClassLibrary1.JScript2.js", "application/x-javascript", PerformSubstitution=true)] // 要加 PerformSubstitution 才能在 js 中使用 WebResource
[assembly: System.Web.UI.WebResource("JsClassLibrary1.icon_Right_arrow.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("JsClassLibrary1.icon_Left_arrow.gif", "image/gif")] - 修改 Class1.cs
繼承 WebControl 覆寫 OnPreRender 用 ClientScriptManager 註冊(RegisterClientScriptResource) JS 檔。 - 建置後產生 JsClassLibrary1.dll
ASP.NET Web 應用程式
新增專案:Visual C#\Web\ASP.NET Web 應用程式
- 專案加入參考 JsClassLibrary1
- 修改 Default.aspx
加上 <%@ Register Assembly="JsClassLibrary1" Namespace="JsClassLibrary1" TagPrefix="klcintw" %> 即可使用 JScript1.js 或 JScript2.js 的 function。
程式碼
JScript1.js
function klcintw_Alert() {
alert('TEST(<%= WebResource("JsClassLibrary1.icon_Right_arrow.gif")%>)');
}
若在 AssemblyInfo.cs 沒有加上「PerformSubstitution="true"」<%%> 的內容會原原本本的顯示出來。
JScript2.js
function klcintw_Alert2() {
alert('TEST2(<%= WebResource("JsClassLibrary1.icon_Right_arrow.gif")%>)');
}
function klcintw_Image(imgControl, bRight) {
document.getElementById(imgControl).src =
bRight ?
'<%= WebResource("JsClassLibrary1.icon_Right_arrow.gif")%>' :
'<%= WebResource("JsClassLibrary1.icon_Left_arrow.gif")%>';
}
Class1.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace JsClassLibrary1
{
public class Class1 : System.Web.UI.WebControls.WebControl
{
protected override void OnPreRender(EventArgs e)
{
if (this.Page != null)
{
System.Web.UI.ClientScriptManager manager = this.Page.ClientScript;
manager.RegisterClientScriptResource(typeof(Class1), "JsClassLibrary1.JScript1.js");
manager.RegisterClientScriptResource(typeof(Class1), "JsClassLibrary1.JScript2.js");
} // if
base.OnPreRender(e);
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JsDllTest._Default" %>
<%@ Register Assembly="JsClassLibrary1" Namespace="JsClassLibrary1" TagPrefix="klcintw" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<klcintw:Class1 ID="rs1" runat ="server"/>
<input type="button" value="TEST" onclick="klcintw_Alert()" />
<input type="button" value="TEST2" onclick="klcintw_Alert2()" /><br />
<input type="button" value="klcintw_Image(R)" onclick="klcintw_Image('myImg', true)" />
<input type="button" value="klcintw_Image(L)" onclick="klcintw_Image('myImg', false)" />
<img id='myImg' alt="" src="" />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JsDllTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
//Get the type of the class.
Type rsType = typeof(JsClassLibrary1.Class1);
// 取得內嵌圖檔的網址
string imgUrl = cs.GetWebResourceUrl(rsType, "JsClassLibrary1.snap014.png");
}
}
}