Sunday, July 15, 2007

ASP.NET despite its many nice features, has also a lot of nasty ones. Some of them are critical - like a design flaw in the way controls are rendered - by the controls themselves (only in version ASP.NET 2.0 Microsoft introduced the concept of Adapters, but this thing is beyond repair. There is just to much logic already in Render methods of all the existing controls to make using adapters easy). Others are just plain annoying, but we mostly we can live with them and even find workarounds.

Today I simply had enough of the style="border-width:0px;" attribute that ASP.NET adds to every img tag it renders from the Image control. There is strightforward way (that I know of) to remove this thing! Try as you might, there will always be a border-width style attribute on you images with either 0px, or whatever value you assign in you ASPX file. But there is a workaround.

To make the border style gone, we have to create our own Image control (inheriting controls is usually a good thing). In this control we have to resort to a trick of overriding the BorderWidth property by returning anything other than Unit.Empty:

public override Unit BorderWidth
{
    get { return Unit.Pixel(0); }
    set { ; }
}

This will cause the border style not to be rendered at all. Additional side effect which is obvious when looking at the above code is that now it is impossible to set BorderWidth property in any way. The value will alwyas be Unit.Pixel(0). This is also a good thing since we really shouldn't set any of the style properties inline but rather use a css style sheets for this purpose.

Sunday, July 22, 2007 9:02:07 AM (Central European Standard Time, UTC+01:00)
This is just the answer I was looking for, thanks! It reeks of desperation, but Microsoft's 'helpfulness' calls for desperate measures.

I don't think that it's a good thing to prevent the border width from ever being set for the new inherited control, so I've altered your solution slightly:

public override Unit BorderWidth
{
get {
if (base.BorderWidth.IsEmpty) return Unit.Pixel(0);
else return base.BorderWidth;
}
set { ; }
}
Tuesday, July 24, 2007 11:30:55 PM (Central European Standard Time, UTC+01:00)
This was exactly what I was looking for! I also only returned Unit.Pixel if it was indeed empty, but I also allowed the set accessor (just in case, although I agree it shouldn't be inline)

public override Unit BorderWidth
{
get
{
if (base.BorderWidth.IsEmpty)
return Unit.Pixel(0);
else
return base.BorderWidth;
}
set
{
base.BorderWidth = value;
}
}
orengold
Thursday, October 04, 2007 8:39:34 PM (Central European Standard Time, UTC+01:00)
I know the frustrations of dealing with this. I looked up this article in an attempt to find a workaround, however I do understand Microsoft's reasoning in this. If you think about it, adding a hidden style to an html tag is not very orthogonal at all. The image SHOULDN'T have a border by default when there's a style attribute to set it. Microsoft is simply following rules of orthogonality, so that people already familiar with their programming languages will not need to change their way of thought for this. I was surprised myself when I first learned that images had borders by default when I tried to make an image a clickable hyperlink and when I tried to align images for layouts. However, as you guys have down, so too have I accepted this fact about images. I decided to just set the borderwidth to what was in my CSS, however I agree that they should've thought out their asp:image control more carefully and created a way to remove that tag if so desired. -- just my 2 cents though
asty
Wednesday, December 26, 2007 2:43:45 AM (Central European Standard Time, UTC+01:00)
I am really new at all of this and was hoping you could guide me to converting this to VB
Raz
Tuesday, January 08, 2008 11:42:19 AM (Central European Standard Time, UTC+01:00)
Thanks a lot :) I'm happy that found your article fast and didn't spend much time for analyzing the root cause. Thanks.
romb
Tuesday, February 26, 2008 5:21:36 PM (Central European Standard Time, UTC+01:00)
You're my hero. Thanks a million.
Brett
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview