Archives / 2006 / April
  • ASP.NET 1.1 server control for <link> - enabling relative URL paths using tilde "~"

    Here's a simple - but useful Link webcontrol class that supports the "~" tilde syntax for relative paths for the href attribute of the <link> element.

    [DefaultProperty("Text"),ToolboxData("<{0}:Link runat=server Href=\"\" Rel=\"Stylesheet\" Type=\"text/css\"></{0}:Link>")]
    public class Link : System.Web.UI.WebControls.WebControl
        private string _href;

        public string Href
            get { return _href; }
            set { _href = value; }

        protected override void Render(HtmlTextWriter output)
            foreach (string key in this.Attributes.Keys)

    You can then simply drop it in the <head> section of your page (provided you've used the Register directive to register the assembly):

    <cc1:Link id="Link1" runat="server" Type="text/css" Rel="Stylesheet" Href="~/my.css" myattribute="Whatever"></cc1:Link>

    The reason I had to roll my own is that when you add runat="server" for the <link> element, it turns into a HtmlGenericControl instance on the server, which is obviously used for numerous HTML elements, and as such no specific path resolve mechanism is applied to any of its attributes, since the attributes are different per HTML element.

    Hope it helps someone out.