views:

360

answers:

7

So I have an asp.net image tag:

<asp:Image runat="server" ImageUrl="~/Images/img.jpg" width="350px" height="250px" AlternateText="My Image" />

but it's outputting this:

<img src="Images/img.jpg" height="250" width="350" border="0" />

...the XHTML validator reckons that the "border" element shouldn't be there...but it's ASP.Net that's adding it.

I'm sure this question has been asked many times before, but why does asp.net think it needs to add it, especially because it's invalid??

How can this be avoided so that it does validate?

+2  A: 

It's the way that the ASP.NET image web control works. Unfortunately, many of the built in ASP.NET web controls are not standards complaint and will cause a page to not validate. The CSS Friendly Control Adapters are a valiant attempt to "fix" the occasionally invalid and less than semantic HTML generated by the built in web controls.

Your particular issue can be avoided by following the advice in the answer to How to build ImageButton Control Adapter.

ahsteele
Yes, well that's very easy to say, but can it be avoided. At the moment my page doesn't validate because of it...
Paul
+1  A: 

By default, if an <img> element appears within an <a> element, the <img> gets an ugly blue border to signify that it's a link.

border="0" is an old-school (but simple, reliable and stand-alone) way to prevent this.

RichieHindle
+2  A: 

See this: ASP.NET Borderless Image

Josh Stodola
+1  A: 

It wasn't always invalid. In the old days, we all added the border="0" to avoid the default blue border when the image was linked. Unfortunately, the ASP.NET team hasn't found it worth while to update the Image control along with the web standards.

Btw, the border attribute is far from the only noncompliant HTML that ASP.NET emits.

Tor Haugen
A: 

border="0" was necessary for Netscape 4, which didn't support the usual CSS way of doing it properly. Of course not much in ASP.NET will actually work with Netscape 4, so this attempt at backwards compatibility is pretty pointless.

Incidentally, as well as the ASP.NET controls' markup being poor, it actually still does browser-sniffing in this day and age: it will omit the related but even more invalid ‘border="0"’ on image buttons for browsers it thinks are ‘good’: a selection of whitelisted browsers that does not include the HTML validator.

Browser-sniffing is a Wrong Thing and most of us stopped doing years ago, but ASP.NET still does it by default, unless you use an upstream clientTarget in your page declaration to make it stop. Ugly stuff, MS.

bobince
A: 

Are you using ASP.NET 1.1? Because in 2.0 and above the image tag renders as:

    <img src="/Images/img.jpg" alt="My Image" 
style="height:250px;width:350px;border-width:0px;" />

I've just tested this and the above is what I get (using Opera 10) with a doctype of XHTML 1.0 Transitional. So I'm not sure why you are getting invalid HTML, unless ASP.NET is somehow detecting your browser as "down level".

  • What browser are you using?
  • What version of .net?
  • What doctype do you have?
Dan Diplo
A: 

See http://forums.asp.net/t/1075248.aspx; the last poast solveed my issue

Jean-Luc