为使用Master的ASP.NET Content页面添加CSS样式使用了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);
}
在 Page_Load 方法中,使用上面两个方法来添加样式:
添加内嵌样式
AddInlineStyle("body { padding:10px; margin:5px 0; }");
引用外部样式
AddLinkedStyle("/styles/layout.css");
简单而实用。HtmlGenericControl 是相当有用的类,在ASP.NET中可以用来定制很多输出行为,实在是应该多加利用的好东东。
来至http://westlife063.blog.163.com/blog/static/129942096201042514651713/
本文介绍了如何在使用Master的ASP.NET Content页面中添加CSS样式。由于Content页面本身不包含head标签,因此需要通过编程方式添加内嵌或外部样式。文章提供了具体的实现方法。
7413

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



