開發自訂web控制項時把css,js嵌入成資源

來源:互聯網
上載者:User

1設定檔案的屬性:產生操作--》內嵌資源

2在AssemblyInfo.cs中添加

  [assembly: WebResource("程式集名.檔案名稱", "類型")] 如:

  [assembly: WebResource("HelloWorld.SH.css","text/css")]
  [assembly: WebResource("HelloWorld.SH.js", "text/javascript")]

3重寫控制項類的OnPreRender,添加引用代碼  

    string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "HelloWorld.SH.css");
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = cssUrl;
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            this.Page.Header.Controls.Add(cssLink);
            this.CssClass = "divHW";

            string jsUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "HelloWorld.SH.js");
            Page.ClientScript.RegisterClientScriptInclude("SH", jsUrl);

 

 

相關文章

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.