Geeks With Blogs
ASP.NET Tips & Tricks

Have you tried to validate the page which has HyperLink control with ImageUrl specified? If you specify "Text" attribute, everything will be fine because generated HTML will have specified alt attribute of the image object, but what if you don't want to put any text in there? It would be completely normal to expect that the generated image will have empty "alt" attribute (alt=""), but that is not what will happen and because of that your page will not pass the validation. In order to fix that problem, I've created small Server Control which you can download here: http://www.azraweb.com/files/HyperLinkAlt.rar
Code is very simple and, basicaly, all it does is overriding rendering of the content if you supplied ImageUrl and left Text attribute empty, as you can see yourself:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace HyperLinkAltNamespace
{  
    [DefaultProperty("NavigateUrl")]
    [ToolboxBitmap(typeof(HyperLinkAlt), "HyperLinkAltNamespace.HyperLinkAlt.bmp")]
    [ToolboxData("<{0}:HyperLinkAlt runat=server></{0}:HyperLinkAlt>")]
    public class HyperLinkAlt : HyperLink
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(this.ImageUrl) && string.IsNullOrEmpty(this.Text))
            {
                writer.AddAttribute("alt", "");
            }
            base.RenderContents(writer);
        }
    }
}

Update: This issue is finally solved in ASP.NET 4.0, so you should read this post only if you're coding in ASP.NET 3.5 and below.

Posted on Sunday, September 7, 2008 7:29 PM | Back to top


Comments on this post: Hyperlink, ImageUrl & valid XHTML

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
hi, thanks for sharing this, however it is unnecessary. You just need to provide your hyperlink control with the Text property value, which will be used in the nested image alt attribute.
Left by useless on Feb 12, 2009 11:49 PM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
Thanks for your comment, but if you carefully read my whole post, you'll see that I mentioned that it's possible to put value in the Text property, but that method will not work if you want to get EMPTY alt, so this: Text="" will not work.
Left by kipo on Feb 13, 2009 8:37 AM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
Putting an empty alt goes against the reason for having a mandatory alternative text field and therfore your site may pass validation but why is validation there in the first place?
Left by Alex on Feb 18, 2009 1:33 PM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
@Alex
Thanks for your comment, but I must say that I don't understand your question - are you asking me why do I want to build web site which will follow the standards? Because that's the only reason "why is validation there in the first place".
Left by kipo on Feb 18, 2009 2:09 PM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
I agree with Alex. You may be able to pass certain hxml validators using an empty alt text, but the alt text should never be empty, if you want to follow the standard. It makes no sense to build a custom control that will write an empty alt property. It's much better to fill out the alt text.
Left by Jeppe Andreasen on Jul 22, 2009 4:32 AM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
@Jeppe Andreasen
I totally agree with you that it's much better to fill out the alt than leave it empty. But, the main question isn't that. The main question is: is it better to put empty alt, or not to put alt property at all? Because Hyperlink control doesn't generate alt if you don't specifically write it (whereas HTML img control does - try to put HTML img control from Toolbox to your page and you'll see that the empty alt is generated). So, it's at least, inconsistency between controls (one generates empty alt, second doesnt't), which shouldn't be there.
Left by kipo on Jul 22, 2009 4:48 AM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
@Jeppe Andreasen
And one other thing - if you think that's pointless to use control which generates empty alt attribute, how do you explain "GenerateEmptyAlternateText" attribute of the ASP.NET Image control?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.generateemptyalternatetext.aspx
Left by kipo on Jul 22, 2009 4:54 AM

# re: Hyperlink, ImageUrl & valid XHTML
Requesting Gravatar...
@hungara
Thanks for your comment, but if you're using ASP.NET 4.0, you can ignore this post, since this issue is finally solved.
Left by kipo on Feb 01, 2011 11:41 AM

Your comment:
 (will show your gravatar)


Copyright © Slobodan Brkovic | Powered by: GeeksWithBlogs.net