很多ASP.NET server控件都需要另外的外部资源来实现某些功能,WebResource.axd就是将一些js,jpg,bmp等封装或叫植入到类库里面。

使用WebResource.axd需要注意几点

1.在类库中注册js脚本资源

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

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

  

参考:

http://blog.csdn.net/kingmax54212008/article/details/9917409

https://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscriptmanager.registerclientscriptresource.aspx

05-11 19:58