This documentation is archived and is not being maintained.

HtmlLink Class

Allows programmatic access to the HTML link element on the server.

Namespace:  System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HtmlLink : HtmlControl
<asp:HtmlLink />

The HtmlLink control allows programmatic access to the HTML link element on the server. The HtmlLink control can be used to programmatically specify a cascading style sheet (CSS) reference in a Web page.

The following code example demonstrates using the HtmlLink control to programmatically add a style sheet to a Web page.

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<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.

<html xmlns="" >
  <head id="head1" 
    <title>HtmlLink Example Page</title>
    <form id="form1" 
      <h1>HtmlLink Example Page</h1>
      This is some text in the body of the Web Forms page.

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 }

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0