使用的类 HtmlLink
HtmlLink类的作用是:以编程的方式访问服务器上的html元素
注意:head必须为服务器控件 ,即 <head id="head1" runat="server">
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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);//将动态生成的css添加到head中
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
详细信息请参考MSDN http://msdn.microsoft.com/zh-cn/library/system.web.ui.htmlcontrols.htmllink(v=VS.85).aspx