使用ASP.NET 2.0提供的WebResource管理资源

本文介绍ASP.NET 2.0中WebResources管理模型的应用,通过实例演示了如何在自定义控件中集成图片和脚本资源。具体展示了如何使用属性装配将资源文件与自定义控件关联,并在运行时正确加载这些资源。
ASP.NET 2.0提供的Web Resources管理模型,很好的解决了image、css、script等外部资源的管理问题
在自定义控件中加入(放在最高级namespace外就行)
  [assembly: WebResource("WebCtrl.cutecat.jpg""image/jpg")] 
  [assembly: WebResource("WebCtrl.cutecat.js""application/x-javascript")]

代码:

ContractedBlock.gifExpandedBlockStart.gifWebResource Demo#region WebResource Demo
InBlock.gif
InBlock.gif
using System;
InBlock.gif
using System.Collections.Generic;
InBlock.gif
using System.ComponentModel;
InBlock.gif
using System.Text;
InBlock.gif
using System.Web.UI;
InBlock.gif
using System.Web.UI.WebControls;
InBlock.gif
InBlock.gif[assembly: WebResource(
"WebCtrl.cutecat.jpg""image/jpg")]
InBlock.gif
InBlock.gif
namespace WebCtrl
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    [DefaultProperty(
"Text")]
InBlock.gif    [ToolboxData(
"<{0}:WebCustom runat=server></{0}:WebCustom>")]
InBlock.gif    
public class WebCustom : WebControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string text;
InBlock.gif        
private Image m_Image;
InBlock.gif
InBlock.gif        [Bindable(
true)]
InBlock.gif        [Category(
"Appearance")]
InBlock.gif        [DefaultValue(
"")]
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn text; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ text = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_Image 
= new Image();
InBlock.gif            
this.Controls.Add(m_Image);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_Image.ImageUrl 
= this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");
InBlock.gif            
this.RenderChildren(output);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion

转载于:https://www.cnblogs.com/goldnet/archive/2008/02/22/1077895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值