views:

19

answers:

1

I'm writing a ServerControl in ASP.NET 3.5, and I'm exposing CssClass, so the user can manipulate the visual appearance of the control. My problem is that I want to establish reasonable defaults, so that the user doesn't have to configure CSS unless he wants to change the defaults.

My specific problem is that my control is emitting html divs, that need to display background images. I want the user to be able to specify a different image in CSS, but I want to display a default background image, and I can't make that work.

The entire server control is emitted as a div, with a class name set to the value the user provided in CssClass. The div that needs the background image is enclosed within this outer div, with a class name of its own. I am currently setting the background image in CSS on the page that contains the control:

   <style type="text/css">
      .cssClass .innerDiv {
         background-image: url("http://....");
      }
   </style>

With this the proper image is drawn. But if it's not there, no image is drawn.

What I want is for the ServerControl to emit some CSS that will define these image urls, that would be over-ridden by any css that was added by the user, and for that default CSS to include URLs to images embedded in the ServerControl's assembly.

And I'm not sure of how to do either. Nor, for that matter, am I sure this is the best approach.

Any ideas?

+1  A: 

Hey,

Expose various properties with CSS classes, such as HeaderCssClass, ItemCssClass, if you need more than one style.

Also, you can do a check that if the user has a CSS class name specified, you use that; otherwise, use your default and omit the custom CSS from the control.

In your rendering logic, you can render the right CSS class name as the attribute of the DIV depending on whether the user has specified anything. So you can do:

if (this.HeaderCssClass != null)
   writer.AddAttribute("class", this.HeaderCssClass);
else
   writer.AddAttribute("class", "standard");

writer.RenderBeginTag("div");

And only write out your standard CSS if the HeaderCssClass is null.

Brian
There could be upwards of half-a-dozen styles that might need setting, which would make creating separate properties tedious and error-prone.OTOH, checking to see that the user has specified a CSS class name won't work. If the user includes a CSS class, and doesn't define the background image URL within it, I still need to provide the default.
Jeff Dege
This is how most of the .NET framework control works (though they use the Style class instead of css classes). On some level, if the user forgets the background image url, then that is their responsibility to set that... that's how it works with most custom control frameworks. Don't know what level you are developing for...
Brian