把js文件编译成dll供页面调用的方法

2025-05-29 0 37

1. 在解决方案中添加一个项目:JSControl

2. 在这个项目添加一个js文件(JScript1.js)

脚本的内容:

?

1

2

3
function showAlert(){

alert('Today is a good dary');

}

3. 改变JScript1.js的属性,Build Action为Embedded Resource(嵌入的资源)

4. 在JSControl项目的AssemblyInfo.cs文件中添加一行:(注意JSControl.JScript1.js,JSControl是命名空间,JScript1.js是文件名)

?

1
[assembly: System.Web.UI.WebResource("JSControl.JScript1.js", "application/x-javascript")]

5. 项目中增加一个注册客户端脚本的类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
namespace JSControl

{

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

{

protected override void OnPreRender(EventArgs e)

{

if (this.Page != null)

{

ClientScriptManager manager = this.Page.ClientScript;

manager.RegisterClientScriptResource(typeof(Class1), "JSControl.JScript1.js");

}

base.OnPreRender(e);

}

}

}

6. 在调用js的项目中添加JSControl.dll的引用

7. 要调用脚本的页面注册JSControl.dll

?

1

2

3

4

5

6

7

8

9
<%@ Register Assembly="JSControl" Namespace="JSControl" TagPrefix="zhi" %>

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

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

<zhi:Class1 ID="rs1" runat ="server"/>

</head>

8. 调用

?

1

2

3

4

5

6
<script type="text/javascript">

$(function ()

{

showAlert();

});

</script>

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 把js文件编译成dll供页面调用的方法 https://www.kuaiidc.com/102794.html

相关文章

发表评论
暂无评论