Adding StyleSheets Programmatically in Asp.Net

前端 未结 4 1225
粉色の甜心
粉色の甜心 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: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

提交回复
热议问题