Asp.net 自訂控制項開發相關的幾種嵌入資源解決方案

來源:互聯網
上載者:User

前提: 如下將要介紹的幾種類型資源都要在其屬性頁面視窗, 將 <產生操作> 屬性, 設定為[內嵌資源],

:

► 給自訂控制項添加自訂表徵圖的幾種方案

   方法一:
          直接在自訂控制項項目中添加一個 *.bmp格式的表徵圖檔案, 並將其命名
         與主控制項檔案相同, 副檔名為 .bmp, 比如主控制項檔案名稱為:
         CustomButton.cs, 則表徵圖檔案命名為: CustomButton.bmp . 編譯項目.
         然後在工具箱中添加此控制項就可以看到剛剛設定的表徵圖效果.

   方法二:
           表徵圖檔案名稱與主控制項名稱不同時, 在類前如下設定.

[ToolboxBitmap(typeof(CustomButton), @"CustomButton1.bmp")]
public class CustomButton : CompositeControl, INamingContainer
{
       //….;
}

編譯項目,然後在工具箱中添加此控制項就可以看到剛剛設定的表徵圖效果.

   方法三:
            表徵圖檔案名稱與主控制項名稱不同時, 用本機絕對路徑. 如下:

           [ToolboxBitmap(@"E:\net\CustomButton(測試資源
           檔案)\CustomButton\CustomButton1.bmp")]
         
           編譯項目,然後在工具箱中添加此控制項就可以看到剛剛設定的表徵圖效果.

     
► 自訂控制項嵌入JavaScript 檔案資源

           1. 定義中繼資料屬性
            [assembly: WebResource("CustomButton.CustomButton_JScript.js",
              "application/javascript")]
           namespace CustomButton
           {
                //….;
           }

           其中 WebResource 的參數1為: 控制項命名空間 + . + JS指令檔名稱

      2. 使用

if (!Page.ClientScript.IsClientScriptIncludeRegistered
("MyJavaScript"))
        {
                Page.ClientScript.RegisterClientScriptInclude
               ("MyJavaScript",Page.ClientScript.GetWebResourceUrl
               (this.GetType(), " CustomButton. CustomButton _JScript.js"));
        }

► 自訂控制項內嵌圖片(gif/jpg 等) 檔案資源

        1. 定義中繼資料屬性
          
           [assembly: WebResource("CustomButton.NoPic.gif", "image/gif")]  
           namespace CustomButton
           {
                  //….;
           }

        2. 使用

          string strImageSrc = Page.ClientScript.GetWebResourceUrl
                (this.GetType(), "CustomButton.NoPic.gif");
          System.Web.UI.WebControls.Image img = new
               System.Web.UI.WebControls.Image();
          img.ImageUrl = strImageSrc;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.