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)
    {
        output.WriteBeginTag("link");
        output.WriteAttribute("href",base.ResolveUrl(this.Href));
        foreach (string key in this.Attributes.Keys)
        {
            output.WriteAttribute(key,this.Attributes[key]);
        }
        output.Write(HtmlTextWriter.TagRightChar);
        output.WriteEndTag("link");
    }
}

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.

10 Comments

  • Hi,

    rather than doing this, i'd look into HtmlLink control implementation. It does the same thing, almost. The only think you'd need to do is to modify the HREF attribute by substituting the base value with the new updated one. Less coding, and definately more OOP. :)

  • Sean,



    I should've mentioned that this is a .NET 1.1 solution. The HtmlLink class is the correct class to use when using .NET 2.0, but does not exist in .NET 1.1 or 1.0.



    Cheers,

    Wim

  • Furthermore, the HtmlLink class in ASP.NET 2.0 already supports the &quot;~&quot; syntax for its href attribute, so in ASP.NET 2.0, all you need to do is add runat=&quot;server&quot; to the link element and you can use the tilde in the href without a problem.

  • An alternative in .NET 1.1 is to use the old ASP &lt;% %&gt; syntax:

    &lt;link href='&lt;% =ResolveUrl(&quot;~/my.css&quot;) %&gt;' ...

  • JJ - yep aware of that, but for our needs we had to also dynamically set the stylesheet, hence the need for the control to be a server control. And as I'm sure you're aware, you can't use the classic ASP &lt;% %&gt; syntax in a server control.

  • Good tips, many thx

  • Thanks, it helped me a lot :)

  • Easier way? Use ResolveURL

    <link href='' rel="stylesheet" type="text/css" />

  • Hi there! I know this is kind of off-topic however I needed to ask.
    Does operating a well-established website like yours take a massive amount work?
    I am brand new to blogging however I do write in my diary on
    a daily basis. I'd like to start a blog so I will be able to share my personal experience and thoughts online. Please let me know if you have any recommendations or tips for new aspiring bloggers. Thankyou!

  • Magnificent goods from you, man. I've be mindful your stuff previous to and you're just extremely fantastic.
    I actually like what you've bought right here, certainly like what you're saying and the way in which during which you say it.
    You make it entertaining and you still take care of to keep it wise.
    I can not wait to learn much more from you. That is really
    a terrific web site.

Comments have been disabled for this content.