把js文件编译成dll供页面调用的方法
2014-08-15来源:

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

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

脚本的内容:

1 function showAlert(){

2 alert('Today is a good dary');

3 }

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

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

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

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

01 namespace JSControl

02 {

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

04 {

05 protected override void OnPreRender(EventArgs e)

06 {

07 if (this.Page != null)

08 {

09 ClientScriptManager manager = this.Page.ClientScript;

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

11 }

12 base.OnPreRender(e);

13 }

14 }

15 }

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

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

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

2

3

4

5

6

7

8

9

8. 调用

1

更多信息请查看IT技术专栏

2026上岸·考公考编培训报班

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
推荐信息
Baidu
map