更新时间:2023-11-30 15:17:34
解决方案我目前正在使用:
我创建了一个辅助类:
使用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 =stylesheet /> };
page.Header.Controls.Add(cssFile);
}
}
}
这个帮助类,我所要做的是:
CssAdder.AddCss(〜/ Resources / MainMaster / MainDesign.css,this.Page);
CssAdder.AddCss(〜/ Resources / Styles / MainMaster / MainLayout.css,this.Page);
CssAdder.AddCss(〜/ Resources / Styles / Controls / RightMainMenu.css,this.Page);
// ...
所以我可以根据需要添加一行
它希望它有帮助。
它也适用于母版页和内容页面的关系。 p>
PS:我不知道这个和其他解决方案之间的性能差异,但它看起来更优雅,更容易消费。如果你知道更好的方法,请让我知道。谢谢...
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 may need a lot:
Example Code:
HtmlLink css = new HtmlLink();
css.Href = "css/fancyforms.css";
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Header.Controls.Add(css);
I also use Page.Header.RenderControl()
method but it didn't work either. Object null something error was thrown.
I also used Page.Header.InnerHtml
and InnerText += "<link .... "/>
things but they threw the Literal error which is I think common error.
I used this code :
List<Literal> cssFiles = new List<Literal>();
cssFiles.Add(new Literal() { Text = @"<link href=""" + ResolveUrl("~/Resources/Styles/MainMaster/MainDesign.css") + @""" type=""text/css"" rel=""stylesheet"" />" });
cssFiles.Add(new Literal() { Text = @"<link href=""" + ResolveUrl("~/Resources/Styles/MainMaster/MainLayout.css") + @""" type=""text/css"" rel=""stylesheet"" />" });
AddStyleRange(cssFiles);
private void AddStyleRange(List<Literal> cssFiles)
{
foreach (Literal item in cssFiles)
{
this.Header.Controls.Add(item);
}
}
It worked at first but when I change the pages it stopped working.
I am using Master Page and I am writing these codes on Master.cs
file and also some people recommended to use this.Header
instead of Page.Header
but when I built it throws an error which says I cannot declare that like this.
It shouldn't be that hard to add many styles.
It is getting complicated.
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. If you know better ways, please let me know. Thanks...