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裏添加一句:
我們可以看msdn裏有WebResource的參數說明:第一個是資源的名字,第二個是資源的mime-type名。
其實這個語句放在任何cs文件裏,保證放在最高級namespace外就行。
然後在程序中調用如下:
GetWebResourceUrl的第一個參數是用戶定義的類型(這個是用來確定assembly用的),第二個參數是資源名。
上面的語句返回給browser的代碼是:
其中的src就是GetWebesourceUrl執行後返回的,它有3個參數(這裏的&被解析成了&,不過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影射。
使用示例代碼如下:
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);
}
}
}