在asp.net中,把javascript,CSS,image等资源内嵌到DLL使用

ASP.NET(1.0/1.1)给我们提供了一个开发WebControl的编程模型,于是我们摆脱了asp里面的include模式的复用方式。不过 1.0/1.1提供的Web控件开发模型对于处理没有image、css等外部资源的组件还算比较得心应手,script虽然很多时候也是外部资源,但在 开发控件的时候我们习惯把script使用Page.Register...Script()来嵌入模块,因为紧凑的东西更便于我们复用,用一个dll就 可以解决问题又何必要节外生枝呢。

         ASP.NET 2.0提供的Web Resources管理模型,很好的解决了image、css、script等外部资源的管理问题。现在只需要在solution explorer把资源文件的build action属性设为Embedded Resource。然后在assemblyinfo.cs里添加一句:

[assembly: WebResource( " WebCtrl.cutecat.jpg " , " image/jpg " )]

        我们可以看msdn里有WebResource的参数说明:第一个是资源的名字,第二个是资源的mime-type名。
    其实这个语句放在任何cs文件里,保证放在最高级namespace外就行。

        然后在程序中调用如下:

m_Image.ImageUrl = this .Page.GetWebResourceUrl( typeof (WebCustom), " WebCtrl.cutecat.jpg " );

        GetWebResourceUrl的第一个参数是用户定义的类型(这个是用来确定assembly用的),第二个参数是资源名。

        上面的语句返回给browser的代码是:

< img src ="WebResource.axd?a=pWebCtrl&amp;r=WebCtrl.cutecat.jpg&amp;t=632390947985312500" style ="border-width:0px;" />

        其中的src就是GetWebesourceUrl执行后返回的,它有3个参数(这里的&被解析成了&amp;,不过IIS也认的),第 一个参数a是就是通过typeof(WebCustom)来确定的assembly的名字,第二个参数r很明显就是资源的名字了,第三个参数t是一个a所 指的assembly的timestamp。这个t是为了让资源的引用能享用browser缓存的优化,因为IE对相同的url有自己的cache机制。 又因为这个r同时又是用户assembly文件的timestamp,如果用户更新了代码,重新编译后t也会变化,这样也就保证了browser能获得最 新的资源更新。如果我们能确定嵌入资源是确实不用再更新的,我们可以在typeof()里写一个bcl里的类型,比如typeof(string),那么 他将只在freamwork升级后才会变动这个t。

        当然这个WebResource.axd是不存在的,它只是IIS中的一个ISAPI影射。

        使用示例代码如下:

#region WebResource Demo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource(
" WebCtrl.cutecat.jpg " , " image/jpg " )]

namespace WebCtrl
{
        [DefaultProperty(
" Text " )]
        [ToolboxData(
" <{0}:WebCustom runat=server></{0}:WebCustom> " )]
    
public class WebCustom : WebControl
        {
        
private string text;
        
private Image m_Image;

            [Bindable(
true )]
            [Category(
" Appearance " )]
            [DefaultValue(
"" )]
        
public string Text
            {
            
get { return text; }
            
set { text = value; }
            }

        
protected override void CreateChildControls()
            {
                m_Image
= new Image();
            
this .Controls.Add(m_Image);
            }

        
protected override void Render(HtmlTextWriter output)
            {
                m_Image.ImageUrl
= this .Page.ClientScript.GetWebResourceUrl( typeof (WebCustom), " WebCtrl.cutecat.jpg " );
            
this .RenderChildren(output);
            }
        }
}
#endregion

JS

1.向项目中添加Jscript文件
//script_1.js-----
function doClick1()
{
       alert("OK1_wufeng");
}
//script_2.js-----
function doClick2()
{
       alert("OK2");
}

2.解决方案资源管理器中,右键查看script_1.js和script_2.js的属性,把高级中的“生成操作”属性设置成“嵌入的资源”。

3.向AssemblyInfo.cs文件中添加如下行:(注意域名wf.ClientScriptResourceLabel)
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_1.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_2.js", "application/x-javascript")]

4.向项目中添加一个类, 实例:
using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;

namespace wf.ClientScriptResourceLabel
{
       public class ClientScriptResourceLabel : System.Web.UI.WebControls.WebControl
       {
           //调用脚本资源
           protected override void OnPreRender(EventArgs e)
           {
               if (this.Page != null)
               {
                   this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_1.js");
                   this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_2.js");
               }
               base.OnPreRender(e);
           }


           /// <summary>
           /// 呈现控件的方法RenderContents
           /// </summary>
           protected override void RenderContents(HtmlTextWriter output)
           {
               output.AddAttribute("id", "1");
               output.AddAttribute("type", "checkbox");
               output.AddAttribute("value", "测试1");
               output.AddAttribute("onclick", "javascript:doClick1();");
               output.RenderBeginTag(HtmlTextWriterTag.Input);
               output.RenderEndTag();

               output.AddAttribute("id", "2");
               output.AddAttribute("type", "checkbox");
               output.AddAttribute("value", "测试2");
               output.AddAttribute("onclick", "javascript:doClick2();");
               output.RenderBeginTag(HtmlTextWriterTag.Input);
               output.RenderEndTag();

               base.RenderContents(output);
           }
       }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值