Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

HtmlLink Class

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)

public class HtmlLink : HtmlControl
public class HtmlLink extends HtmlControl
public class HtmlLink extends HtmlControl

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#"%>
<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 }
System.Object
   System.Web.UI.Control
     System.Web.UI.HtmlControls.HtmlControl
      System.Web.UI.HtmlControls.HtmlLink
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 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.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.