<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Horn Network &#187; PerformSubstitution</title>
	<atom:link href="http://klcin.tw/net/tag/performsubstitution/feed" rel="self" type="application/rss+xml" />
	<link>http://klcin.tw/net</link>
	<description>Horn Network (.NET, ASP.NET, C#, VB.NET, JavaScript, Ubuntu, Android ...)</description>
	<lastBuildDate>Fri, 04 Mar 2011 07:25:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>[ASP.NET]將 Script 和 Image 封裝在 dll 內</title>
		<link>http://klcin.tw/net/script-image-webresource-dll</link>
		<comments>http://klcin.tw/net/script-image-webresource-dll#comments</comments>
		<pubDate>Sat, 28 Mar 2009 04:01:45 +0000</pubDate>
		<dc:creator>klcintw</dc:creator>
				<category><![CDATA[ASP.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[檔案下載]]></category>
		<category><![CDATA[GetWebResourceUrl]]></category>
		<category><![CDATA[PerformSubstitution]]></category>
		<category><![CDATA[RegisterClientScriptResource]]></category>
		<category><![CDATA[WebResource]]></category>

		<guid isPermaLink="false">http://klcin.tw/net/?p=115</guid>
		<description><![CDATA[本文介紹：如何將靜態檔案（如：.html files, .css files, image files, and script files）封裝在 dll 內及如何使用。（.NET 2.0 以上適合） 優點： 可避免靜態檔案放錯位置而造成錯誤。 許多第三方商用元件也是使用此法。 類別庫（DLL） 新增專案：Visual C#＼Windows＼類別庫 加入參考 System.Web 為了要繼承 System.Web.UI.WebControls.WebControl 及相關 functions 。 加入所需的靜態檔案，並將其建置動作設為「內嵌資源」 （如：下圖的 icon_Left_arrow.gif, icon_Right_arrow.gif , JScript1.js , JScript2.js） &#160; 修改 AssemblyInfo.cs為每一靜態檔案登記[assembly: System.Web.UI.WebResource(&#34;JsClassLibrary1.JScript1.js&#34;, &#34;application/x-javascript&#34;)] [assembly: System.Web.UI.WebResource(&#34;JsClassLibrary1.JScript2.js&#34;, &#34;application/x-javascript&#34;, PerformSubstitution=true)]&#160; // 要加 PerformSubstitution 才能在 js 中使用 WebResource [assembly: System.Web.UI.WebResource(&#34;JsClassLibrary1.icon_Right_arrow.gif&#34;, &#34;image/gif&#34;)] [assembly: System.Web.UI.WebResource(&#34;JsClassLibrary1.icon_Left_arrow.gif&#34;, [...]]]></description>
			<content:encoded><![CDATA[<h1></h1>
<p><strong>本文介紹</strong>：如何將靜態檔案（如：.html files, .css files, image files, and script files）封裝在 dll 內及如何使用。（.NET 2.0 以上適合）</p>
<p>優點：</p>
<ol>
<li>可避免靜態檔案放錯位置而造成錯誤。 </li>
<li>許多第三方商用元件也是使用此法。</li>
</ol>
<p>  <span id="more-115"></span><br />
<h2>類別庫（DLL）</h2>
<p>新增專案：Visual C#＼Windows＼類別庫</p>
<ol>
<li>加入參考 <strong>System.Web</strong>       <br />為了要繼承 System.Web.UI.WebControls.WebControl 及相關 functions 。 </li>
<li>加入所需的靜態檔案，並將其<strong>建置動作</strong>設為「<strong><font color="#ff0000">內嵌資源</font></strong>」       <br />（如：下圖的 icon_Left_arrow.gif, icon_Right_arrow.gif , JScript1.js , JScript2.js）       <br />&#160;<a href="http://klcin.tw/net/wp-content/uploads/2009/03/snap019.png"><img title="內嵌資源" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="273" alt="內嵌資源" src="http://klcin.tw/net/wp-content/uploads/2009/03/snap019-thumb.png" width="411" border="0" /></a> </li>
<li>修改 AssemblyInfo.cs<br />為每一靜態檔案登記<br />[assembly: System.Web.UI.WebResource(&quot;JsClassLibrary1.JScript1.js&quot;, &quot;application/x-javascript&quot;)]       <br />[assembly: System.Web.UI.WebResource(&quot;JsClassLibrary1.JScript2.js&quot;, &quot;application/x-javascript&quot;<strong><font color="#ff0000">, PerformSubstitution=true</font></strong>)]&#160; <font color="#008000">// 要加 PerformSubstitution 才能在 js 中使用 WebResource</font>      <br />[assembly: System.Web.UI.WebResource(&quot;JsClassLibrary1.icon_Right_arrow.gif&quot;, &quot;image/gif&quot;)]       <br />[assembly: System.Web.UI.WebResource(&quot;JsClassLibrary1.icon_Left_arrow.gif&quot;, &quot;image/gif&quot;)] </li>
<li>修改 Class1.cs      <br />繼承 WebControl 覆寫 OnPreRender 用 ClientScriptManager 註冊(<a href="http://msdn.microsoft.com/zh-tw/library/system.web.ui.clientscriptmanager.registerclientscriptresource(VS.80).aspx" target="_blank">RegisterClientScriptResource</a>) JS 檔。 </li>
<li>建置後產生 JsClassLibrary1.dll      <br /><a href="http://klcin.tw/net/wp-content/uploads/2009/03/snap021.png"><img title="dll" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="271" alt="dll" src="http://klcin.tw/net/wp-content/uploads/2009/03/snap021-thumb.png" width="286" border="0" /></a>&#160; </li>
</ol>
<h2>ASP.NET Web 應用程式</h2>
<p>新增專案：Visual C#＼Web＼ASP.NET Web 應用程式</p>
<ol>
<li>專案加入參考 JsClassLibrary1</li>
<li>修改 Default.aspx     <br />加上 &lt;%@ Register Assembly=&quot;JsClassLibrary1&quot; Namespace=&quot;JsClassLibrary1&quot; TagPrefix=&quot;klcintw&quot; %&gt; 即可使用 JScript1.js 或 JScript2.js 的 function。</li>
</ol>
<p>&#160;</p>
<h2>程式碼 </h2>
<h3>JScript1.js</h3>
<p>
<pre class="brush: js">function klcintw_Alert() {
  alert(&#039;TEST(&lt;%= WebResource(&quot;JsClassLibrary1.icon_Right_arrow.gif&quot;)%&gt;)&#039;);
}</pre>
</p>
<p>若在 AssemblyInfo.cs 沒有加上「PerformSubstitution="true"」<%%> 的內容會原原本本的顯示出來。 </p>
<h3>JScript2.js</h3>
<p>
<pre class="brush: js">function klcintw_Alert2() {
  alert(&#039;TEST2(&lt;%= WebResource(&quot;JsClassLibrary1.icon_Right_arrow.gif&quot;)%&gt;)&#039;);
}

function klcintw_Image(imgControl, bRight) {
  document.getElementById(imgControl).src =
    bRight ?
      &#039;&lt;%= WebResource(&quot;JsClassLibrary1.icon_Right_arrow.gif&quot;)%&gt;&#039; :
      &#039;&lt;%= WebResource(&quot;JsClassLibrary1.icon_Left_arrow.gif&quot;)%&gt;&#039;;
}
</pre>
</p>
<h3>Class1.cs</h3>
<p>
<pre class="brush: c#">using System;
using System.Collections.Generic;
using System.Text;

namespace JsClassLibrary1
{
  public class Class1 : System.Web.UI.WebControls.WebControl
  {
    protected override void OnPreRender(EventArgs e)
    {
      if (this.Page != null)
      {
        System.Web.UI.ClientScriptManager manager = this.Page.ClientScript;
        manager.RegisterClientScriptResource(typeof(Class1), &quot;JsClassLibrary1.JScript1.js&quot;);
        manager.RegisterClientScriptResource(typeof(Class1), &quot;JsClassLibrary1.JScript2.js&quot;);
      } // if
      base.OnPreRender(e);
    }
  }
}
</pre>
</p>
<h3>Default.aspx</h3>
<p>
<pre class="brush: html">&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;JsDllTest._Default&quot; %&gt;
&lt;%@ Register Assembly=&quot;JsClassLibrary1&quot; Namespace=&quot;JsClassLibrary1&quot; TagPrefix=&quot;klcintw&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head runat=&quot;server&quot;&gt;
  &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
  &lt;div&gt;
  &lt;klcintw:Class1 ID=&quot;rs1&quot;  runat =&quot;server&quot;/&gt;
  &lt;input type=&quot;button&quot; value=&quot;TEST&quot; onclick=&quot;klcintw_Alert()&quot; /&gt;
  &lt;input type=&quot;button&quot; value=&quot;TEST2&quot; onclick=&quot;klcintw_Alert2()&quot; /&gt;&lt;br /&gt;
  &lt;input type=&quot;button&quot; value=&quot;klcintw_Image(R)&quot; onclick=&quot;klcintw_Image(&#039;myImg&#039;, true)&quot; /&gt;
  &lt;input type=&quot;button&quot; value=&quot;klcintw_Image(L)&quot; onclick=&quot;klcintw_Image(&#039;myImg&#039;, false)&quot; /&gt;
  &lt;img id=&#039;myImg&#039; alt=&quot;&quot; src=&quot;&quot; /&gt;
  &lt;/div&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</p>
<h3>Default.aspx.cs</h3>
<p>
<pre class="brush: c#">using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JsDllTest
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      // Get a ClientScriptManager reference from the Page class.
      ClientScriptManager cs = Page.ClientScript;
      //Get the type of the class.
      Type rsType = typeof(JsClassLibrary1.Class1);
      // 取得內嵌圖檔的網址
      string imgUrl = cs.GetWebResourceUrl(rsType, &quot;JsClassLibrary1.snap014.png&quot;);
    }
  }
}
</pre>
</p>
<p>&#160;</p>
<h4>檔案下載</h4>
<ul>
<li><a href='http://www.box.net/shared/2ixjdc6yyx' target='_blank'>程式碼（Box.net）</a></li>
</ul>
<h4>參考資料：</h4>
<ul>
<li>[Microsoft] <a href="http://support.microsoft.com/kb/910442" target="_blank">Working with Web Resources in ASP.NET 2.0</a> </li>
<li><a href="http://www.cnblogs.com/cry/archive/2009/03/27/1423243.html" target="_blank">如何把.js文件编译成dll供页面调用？ &#8211; 一只鞋</a> </li>
<li><a href='http://jackielin.cnblogs.com/archive/2005/11/29/286570.html' target='_blank'>在自定义Server Control中捆绑JS文件 Step by Step &#8211; BLACK JACK</a></li>
<li><a href="http://subocheng.blogspot.com/2007/11/aspnet-20-webresourceaxd-and-browser.html" target="_blank">Mr. NFrankenstein: ASP.NET 2.0 WebResource.axd and browser caching</a> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://klcin.tw/net/script-image-webresource-dll/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

