开发者

CssClass and default images in ServerContol

开发者 https://www.devze.com 2022-12-24 07:42 出处:网络
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

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 mak开发者_运维问答e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号