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