I'm working with ASP.Net and trying to style an ImageButton. I tried to do this via CSS only to discover that for some reason, .Net writes out an inline style setting the border width to 0 automatically. This overrides the border width from CSS, so I have to set it manually in the codebehind.

My question is, should I do all the styling in the codebehind to keep it all in one place, or only do the border width there and do the rest in a style sheet?

For clarification, I mean all the styling for that object not for the whole site.

  • 2
    sounds like a bug to me. – gbjbaanb Nov 28 '12 at 13:57
  • 2
    @gbjbaanb Yes, it is a bug in .Net – Kevin Nov 28 '12 at 14:12
  • 1
    This may help:forums.asp.net/t/1230264.aspx/1 – NoChance Nov 28 '12 at 14:15
  • 2
    I think the proper term is design flaw -- setting the image at 0 made sense in 2001 when they designed the control. – Wyatt Barnett Nov 28 '12 at 14:47
  • And this is why I refuse contracts working on Webforms. (Same way for Winforms and VB.NET. – Michael Brown Nov 28 '12 at 14:51

It seems like doing all of your styling in code-behind because of one particular bug issue is like throwing out the baby with the bathwater.

I say you should fix the one issue in code-behind, with a comment explaining why it's done that way, and perhaps a comment in the CSS file explaining why it's not done there. Then go on with life. :)

  • I mean just the styling for that one object, not the entire site. Added some clarificatio to the question – Kevin Nov 28 '12 at 15:11
  • @Kevin Even so, I would give the same answer. – Eric King Nov 28 '12 at 15:11
  • This is actually what I ended up doing. Setting the width in the code and rest of styling in css with comments in both places explaining it – Kevin Nov 28 '12 at 19:20

The best way may be to try and remove the inline style with JavaScript, though I'm not 100% sure that would work. The biggest problem with doing all the styling in the code behind is that you must redeploy the actual code for a style change, which may or may not be an issue, if you put all the other styling in the style sheet it can be changed only by replacing that file. When you are working with CSS though getting it to work in any way is a victory, your options all suck in some way, splitting up styling information or working with CSS in the codebehind or hacking it together with JavaScript.


Does it need to be an imagebutton? Personally I'd use the more accessible real button and then use CSS to put the background image in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.