http://w1.org.cn/web/programming/cmeta.html
首页 >> 编程知识
C#动态增加页面头部标签(如CSS,Meta等)、将Repeater和一个XmlNodeList绑定
C#动态增加页面头部标签(如CSS,Meta等)
C#动态增加页面头部标签(如CSS,Meta等)
C# 复制代码%@ Page Language="C#"%
script runat="server">
protected void Page_Init(object sender, EventArgs e)
{
// Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "~/StyleSheet.css";
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);
}
/script>
script>
/script>
html>
head id="head1"
runat="server">
title>HtmlLink Example Page/title>
/head>
body>
form id="form1"
runat="server">
h1>HtmlLink Example Page</h1>
This is some text in the body of the Web Forms page.
/form>
/body>
/html>
若要运行上述代码示例,需要一个名为 Stylesheet.css 的联级样式表 (CSS),该样式表包含以下代码并保存在网站所在的目录中。
C# 复制代码
body {
padding-left: 11em;
font-family: Verdana, "Times New Roman",
Times, serif;
color: blue;
background-color: silver }
h1 {
font-family: Helvetica, Geneva, Arial,
SunSans-Regular, sans-serif;
color: purple }
body {
padding-left: 11em;
font-family: Verdana, "Times New Roman",
Times, serif;
color: blue;
background-color: silver }
h1 {
font-family: Helvetica, Geneva, Arial,
SunSans-Regular, sans-serif;
color: purple }
C#动态增加Meta标签:
this.Page.Title = str4;
HtmlMeta child = new HtmlMeta();
child.Name = "Description";
child.Content = str5;
this.Page.Header.Controls.Add(child);
HtmlMeta meta2 = new HtmlMeta();
meta2.Name = "keywords";
meta2.Content = str6;
this.Page.Header.Controls.Add(meta2);
HtmlMeta meta3 = new HtmlMeta();
meta3.HttpEquiv = "Content-Type";
meta3.Content = "text/html; charset=gb2312";
this.Page.Header.Controls.Add(meta3);
HtmlMeta meta4 = new HtmlMeta();
meta4.HttpEquiv = "Content-Language";
meta4.Content = "zh-cn";
this.Page.Header.Controls.Add(meta4);
如果其中XmlNode的属性值有些为null(从数据库中读取出来为null值),那么在使用< %# ((System.Xml.XmlNode)Container.DataItem).Attributes["AttributeName"].Value % > 显示数据的时候,页面会报错“没有引用到实例上”,因为是对null值进行了.Value的属性引用。
有人碰到过类似问题没?怎么解决?
< %# ((System.Xml.XmlNode)Container.DataItem).Attributes["AttributeName"].Value==null?"": ((System.Xml.XmlNode)Container.DataItem).Attributes["AttributeName"].Value% >
这些写倒是没有问题,就是感觉在aspx页面里面又多出了一大堆代码,不爽
友情合作伙伴:第一网络!本文出自:http://w1.org.cn/web/programming/cmeta.html