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;
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.Drawing;
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("value", "測試2");
output.AddAttribute("onclick", "javascript:doClick2();");
base.RenderContents(output);
}
部落格園大道至簡
<a href="http://www.cnblogs.com/jams742003/" target="_blank">http://www.cnblogs.com/jams742003/</a>
轉載請注明:部落格園