HtmlLink Class
.NET Framework 2.0
Note: This class is new in the .NET Framework version 2.0.
Allows programmatic access to the HTML <link> element on the server.
Namespace: System.Web.UI.HtmlControls
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
The following code example demonstrates using the HtmlLink control to programmatically add a style sheet to a Web page.
<%@ 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>
To run the preceding code example, you need a cascading style sheet (CSS) named Stylesheet.css, containing the following code, saved in the same directory as your Web site.
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 }
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
System.Object
System.Web.UI.Control
System.Web.UI.HtmlControls.HtmlControl
System.Web.UI.HtmlControls.HtmlLink
System.Web.UI.Control
System.Web.UI.HtmlControls.HtmlControl
System.Web.UI.HtmlControls.HtmlLink
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
RSS link
Add an RSS link to your page's head element programmatically:
// Create a link control
HtmlLink rssLink = new HtmlLink();
rssLink.Attributes.Add("rel", "alternate");
rssLink.Attributes.Add("type", "application/rss+xml");
rssLink.Attributes.Add("title", "Feed Title");
rssLink.Href = "http://url/to.rss";
// Add the link to the page header
Page.Header.Controls.Add(rssLink);
(Requires your page or master page to have a head runat="server")
- 6/14/2006
- Kirk Jackson - ASP.NET MVP
- 6/14/2006
- Kirk Jackson - ASP.NET MVP