Wimdows.NET

Wim's .NET blog

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.
Posted: Apr 27 2006, 11:56 AM by Wim | with 8 comment(s)
Filed under: , ,

Comments

Sean said:

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. :)
# April 27, 2006 9:15 AM

Wim Hollebrandse said:

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
# April 27, 2006 9:31 AM

Wim Hollebrandse said:

Furthermore, the HtmlLink class in ASP.NET 2.0 already supports the "~" syntax for its href attribute, so in ASP.NET 2.0, all you need to do is add runat="server" to the link element and you can use the tilde in the href without a problem.
# April 27, 2006 10:43 AM

JJ said:

An alternative in .NET 1.1 is to use the old ASP <% %> syntax:
<link href='<% =ResolveUrl("~/my.css") %>' ...
# April 27, 2006 12:04 PM

Wim Hollebrandse said:

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 <% %> syntax in a server control.
# April 27, 2006 12:12 PM

Carlo Bertini said:

Good tips, many thx

# July 5, 2006 6:20 AM

Javier Callico said:

Good idea. I used it as start point for a culture-aware link custom control. I'll post the code on my website http://www.callicode.com/

# March 14, 2007 12:40 PM

Cristiano said:

Thanks, it helped me a lot :)

# February 21, 2008 6:21 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)