將js文件編譯成dll在Asp.Net中調用

1.在解決方案中添加一個新項目(類庫:JSControl);

2.在新項目中添加要用的JS腳本(JScript1.js);

3.選擇js文件將其屬性的“生成操作”設置成“嵌入的資源”;

4.在JSControl項目的AssemblyInfo.cs文件添加一行:(注意JSControl.JScript1.js,JSControl是js所在項目的命名空間)
 
//定義組件內的內嵌資源的元數據屬性[assembly: System.Web.UI.WebResource("JSControl.JScript1.js", "application/x-javascript")]

5.在JSControl項目中添加一個註冊客戶端腳本的類:ReferenceScript.cs
 
namespace JSControl{    public class ReferenceScript : System.Web.UI.WebControls.WebControl    {        protected override void OnPreRender(EventArgs e)        {            if (this.Page != null)            {                ClientScriptManager manager = this.Page.ClientScript;                manager.RegisterClientScriptResource(typeof(ReferenceScript), "JSControl.JScript1.js");            }            base.OnPreRender(e);        }    }}6.編譯並生成Dll文件;7.在Asp.Net站點中引用以上生成的Dll;8.在要調用腳本的頁面註冊JSControl.dll 
 
<%@ Register Assembly="JSControl" Namespace="JSControl" TagPrefix="zhi" %>9.引用腳本源
 
10.在頁面的最後寫相關的腳本進行調用以上的腳本函數;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章