October 2007
Posted on the 22nd at 9:47 PM CST
ASP.NET Image Control - Border Width Inline Style
FiledFiled under ASP.NET

I've been running a fine-toothed comb through the rendered markup of this blog lately in search for anything that could use a good cleaning.  While doing that, I noticed that a lot of the images in code had an inline style attribute, setting the border-width to 0px.  I couldn't figure out how in the heck this was happening.  I ended up at this WebmasterWorld forum thread.  Apparently ASP.NET does this by design, I am not sure why.  The inline style serves no purpose, in my opinion.  And I made it a rule of thumb to not use inline styles on this blog, so I had to do something about it.

In the forum thread it says you can change your image controls to standard <img> tags with the runat="server" attribute and it will correct the problem.  That is definitely an option, but who really wants to have to locate all the image controls in your entire application and change the markup and code-behind accordingly?  Not me.  So, I decided to whip up my own Image control that inherits the base Image and properly sets the border-width...

Public Class BorderlessImage
    Inherits System.Web.UI.WebControls.Image

    Public Overrides Property BorderWidth() As System.Web.UI.WebControls.Unit
        Get
            If MyBase.BorderWidth.IsEmpty Then
                Return Unit.Pixel(0)
            Else
                Return MyBase.BorderWidth
            End If
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            MyBase.BorderWidth = value
        End Set
    End Property
End Class

This gave me the perfect opportunity to excercise a feature of ASP.NET called tag mapping, which I learned from a post by Mads Kristensen.  Just modify your web.config as illustrated below to override all existing Image controls with the BorderlessImage...

  <system.web>
    <pages>
      <tagMapping>
        <add tagType="System.Web.UI.WebControls.Image" mappedTagType="BorderlessImage"/>
      </tagMapping>
    </pages>
  </system.web>
 
And that's all there is to it!  I really would like to know why ASP.NET sticks that inline style in there by default.  If anybody out there has a clue, please enlighten me!
Comments (4)
Permalink Comment from Matt on December 19th, 2007 at 6:58 AM
Hi thanks for putting this up. I found it very useful after findin out why my styles were not applying correctly. I converted it to c# and now my sites all look how they should.
Permalink Comment from Jules on December 29th, 2007 at 9:51 PM
Thank you. This tip worked a treat (after spending a good couple of hours scratching my head and failing with other alternatives)
Permalink Comment from Peter on January 28th, 2008 at 4:59 PM
Yes I saw some discussions about this on forums.asp.net. Your solution was the most elegant.

Also I want to let you know that I think the look and feel of your Blog is very well done indeed. You have put a lot of time into the style and it shows. Perhaps the best looking Blog I have seen...

Cheers, Peter Mead
Permalink Comment from Josh StodolaEmail on January 28th, 2008 at 5:09 PM
Peter, thank you so much! That is the nicest compliment I have received on this blog thus far. And I've been having a bad day too, you really just lifted my spirits!

Guess What?

There are a few basic guidelines you should be aware of before leaving a comment…

  • If you choose to display your email address, it will not be detected by spam bots
  • Comments are limited to 3,000 characters; so far you have used none of them
  • HTML will be encoded; links and line breaks will be converted automatically
  • Comments containing five or more links will be subject to moderation

Have Your Say

← Answer this to prove you are human
 
 

Chill Out…

No Trackbacks