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