使用了Master的ASP.NET Content页面无法直接引用外部样式或内嵌样式,因为CSS样式必须出现在HTML的head标签内,而Content页面自身是不能包含head的。不过通过编程,很容易做到这一点,以下就是解决方案(也可以用相同的手段来添加其他HTML元素)。先定义以下两个方法:
- 内嵌样式支持
protected void AddInlineStyle(string style)
{
HtmlGenericControl node = new
HtmlGenericControl("style");
node.Attributes.Add("type", "text/css");
node.InnerText = style;
Page.Header.Controls.Add(node);
} - 外部样式支持
protected void AddLinkedStyle(string url)
{
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("href", url);
Page.Header.Controls.Add(link);
}
本文介绍如何在ASP.NET Content页面中添加内嵌样式和外部样式。由于Content页面不能直接包含head标签,因此需要通过编程方式将样式添加到Page.Header中。
7416

被折叠的 条评论
为什么被折叠?



