Adding StyleSheets Programmatically in Asp.Net

前端 未结 4 1223
粉色の甜心
粉色の甜心 2020-12-03 13:51

I want to add StyleSheets programmatically in the head section but one of the examples I saw seemed to need to many lines of code to add just one style sheet even though I m

相关标签:
4条回答
  • 2020-12-03 14:07

    I define a generic HTML <link> and set the href attribute programmatically.

    For example, in the page <head> I have:

    <link id="cssStyle" runat="server" rel="stylesheet" type="text/css" />.
    

    Then in Page_Load set the Href property of cssStyle:

    cssStyle.Href = "path/to/Styles.css";
    

    Seems a bit cleaner with the upside of having the design control over placing the <link> in the desired order.

    0 讨论(0)
  • 2020-12-03 14:10

    I'll paste the thing which worked for me:

    HtmlLink link = new HtmlLink();
    //Add appropriate attributes
    link.Attributes.Add("rel", "stylesheet");
    link.Attributes.Add("type", "text/css");
    link.Href = "/Resources/CSS/NewStyles.css";
    link.Attributes.Add("media", "screen, projection");
    //add it to page head section
    this.Page.Header.Controls.Add(link);
    

    Even I searched a lot on this, I'd to add a overriding style sheet when a button is clicked. I used the above code and it worked perfectly to me.

    0 讨论(0)
  • 2020-12-03 14:14

    I went a step over, I wanted a method that makes me impossible to add include duplicates, something like ClientScriptManager.RegisterClientScriptInclude(). The solution is to give an ID to the control added in the Header section.

    if (!String.IsNullOrEmpty(Key))
         if (Page.Header.FindControl(Key) != null) return;
    
    HtmlLink link = new HtmlLink();
    if (!String.IsNullOrEmpty(Key)) link.ID = Key;
    link.Href = StyleUrl; 
    link.Attributes.Add("type", "text/css"); 
    link.Attributes.Add("rel", "stylesheet");
    Page.Header.Controls.Add(link);
    

    For the complete article I wrote: http://www.idea-r.it/Blog.aspx?Article=49

    0 讨论(0)
  • 2020-12-03 14:15

    Okay, here is the solution I am currently using :

    I created a helper class :

    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace BusinessLogic.Helper
    {
        public class CssAdder
        {
            public static void AddCss(string path, Page page)
            {
                Literal cssFile = new Literal() { Text = @"<link href=""" + page.ResolveUrl(path) + @""" type=""text/css"" rel=""stylesheet"" />" };
                page.Header.Controls.Add(cssFile);
            }
        }
    }
    

    and then through this helper class, all I have to do is :

    CssAdder.AddCss("~/Resources/Styles/MainMaster/MainDesign.css", this.Page);
    CssAdder.AddCss("~/Resources/Styles/MainMaster/MainLayout.css", this.Page);
    CssAdder.AddCss("~/Resources/Styles/Controls/RightMainMenu.css", this.Page);
    //...
    

    So I can add as much as I want with one line of simple code.

    It also works with Masterpage and content page relationships.

    Hope it helps.

    P.S: I don't know the performance difference between this and other solutions but it looks more elegant and easy to consume.

    0 讨论(0)
提交回复
热议问题