C#中JS封裝調用

來源:互聯網
上載者:User

 

 

通常在ASP.NET,我們常常會用到JS指令碼來實現頁面上的很多操作。在頁面中引用JS的方法也是有好幾種。

這裡來介紹一下,如何將JS指令碼內嵌到組件(Assembly)中,即DLL中。這樣的做法最大的好處就是能夠保護JavaScript檔案的內容,避免JavaScript類的定義被修改。
下面就以一個樣本來講解如何操作:
1.開啟VS,建立一個WebApplication1項目[這裡建立網站也可以],採用預設設定。

2.在當前解決方案中添加新項目。(類庫: JSLib)

添加後,預設的會有一個Class.cs檔案,我們將其重新命名為:【ReferenceClientScript.cs】,這在後面我們會用它。

3.由於建立的是類庫,所以要添加引用:System.Web,這在後我們會用到。

4.在JSLib中添加一個JS檔案,取預設設定。自動有一個JScript1.js檔案。

5.編寫JS指令碼【JScript.js】。

我們這裡只是一個簡單的示範,所以指令碼代碼盡量簡單。Hello,World!,一段最簡單的類,目的在示範,當然你可以寫的更複雜些。

JSScript.js 代碼:

function Hello(){  

alert("Hello,world!");  

}

6.設定JScript.js屬性->產生操作為【內嵌資源】

7.在JSLib項目的AssemblyInfo.cs檔案最後添加一行代碼:【注意JSLib.JScript1.js,JSLib是js所在項目的命名空間】

[assembly: WebResource("JSLib.JScript1.js", "text/javascript")]

8.編寫JS指令碼註冊類【ReferenceClientScript.cs】。

ReferenceClientScript.cs 代碼:

using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

namespace JSLib  

{  

    public class ReferenceClientScript : System.Web.UI.WebControls.WebControl  

    {  

        protected override void OnPreRender(EventArgs e)  

        {  

            if (this.Page != null)  

            {  

                ClientScriptManager manager = this.Page.ClientScript;  

                manager.RegisterClientScriptResource(typeof(ReferenceClientScript), "JSLib.JScript1.js");  

            }  

            base.OnPreRender(e);  

        }  

    }  

}

9.在WebApplication1中添加項目引用。編譯後產生JSLib.dll

10.在要呼叫指令碼的頁面註冊JSLib.dll

<%@ Register Assembly="JSLib" Namespace="JSLib" TagPrefix="JSLibInstance" %>

11.引用指令碼。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>  

<%@ Register Assembly="JSLib" Namespace="JSLib" TagPrefix="JSLibInstance" %>  

<!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">  

    <asp:ScriptManager ID="ScriptManager1" runat="server">  

    </asp:ScriptManager>  

    <div>      

        <JSLibInstance:ReferenceClientScript id="js1" runat="server"></JSLibInstance:ReferenceClientScript>  

        JS封裝到DLL 並在頁面調用 Demo。<br />  

        <input id="Button1" type="button" value="button" onclick="Hello();" />  

    </div>  

    </form>  

</body>  

</html>

12.完成

13.查看原始碼會發現js引用都被封裝成.axd。如:<script src="/WebResource.axd?d=oJeV8gYSmjAsGIcbJV7ux0vxGpRb1JuUde0qLBlln9w1&t=634125776275000000" type="text/javascript"></script>

<script src="/ScriptResource.axd?d=PpmY9YsXYmWQq6liGNs5LuMNLtDhVsvPqe3CTCI27BNLCLfislppBnoCcro5r89BmQs8Z6mSbRu0fueDIrPViqaI6EEX1sZEHY62mtMOG401&t=7528725e" type="text/javascript"></script>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.