不少ASP.NET server控件都須要另外的外部資源來實現某些功能,WebResource.axd就是將一些js,jpg,bmp等封裝或叫植入到類庫裏面。javascript
使用WebResource.axd須要注意幾點java
1.在類庫中註冊js腳本資源web
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI; [assembly: WebResource("WebResource.dome.js", "text/javascript", PerformSubstitution = true)] namespace WebResource { public class WebDemo { public WebDemo(System.Web.UI.Page p) { string key = base.GetType().ToString(); if (!p.ClientScript.IsClientScriptBlockRegistered(key)) { p.ClientScript.RegisterClientScriptResource(typeof(WebDemo), "WebResource.dome.js"); //這時就是將js文件註冊到頁面,,注意js文件名稱的寫法:命名空間+文件夾名子(若是沒有放在文件夾中能夠省略)+文件名稱 } } } }
dome.js:
function HellowWeb() { alert("我來自類庫中的js文件"); }
dome.js須要修改屬性,「生成操做」=「嵌入的資源」
2.在要使用的頁面
using WebResource;
protected void Page_Load(object sender, EventArgs e) { //註冊js腳本 new WebDemo(this); }
<form id="form1" runat="server"> <input id="Button" onclick="HellowWeb();" type="button" value="button" /> </form>
3.修改Config文件dom
<pages validateRequest="false" enableEventValidation="false" pageBaseType="WebResource.WebDemo,WebResource"> </pages>
<httpHandlers> <add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="true"/> </httpHandlers>
參考:ui
http://blog.csdn.net/kingmax54212008/article/details/9917409this
https://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscriptmanager.registerclientscriptresource.aspxspa