WebResource.axd文件的配置和使用

很多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" οnclick="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

 

转载于:https://www.cnblogs.com/cn2758/p/4683545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值