ServerContol 中的 CssClass 和默认图像
我正在 ASP.NET 3.5 中编写 ServerControl,并且公开 CssClass,以便用户可以操纵控件的视觉外观。我的问题是我想建立合理的默认值,这样用户就不必配置CSS,除非他想更改默认值。
我的具体问题是我的控件正在发出 html div,需要显示背景图像。我希望用户能够在 CSS 中指定不同的图像,但我想显示默认的背景图像,但我无法做到这一点。
整个服务器控件作为 div 发出,类名设置为用户在 CssClass 中提供的值。需要背景图像的 div 包含在这个外部 div 中,并具有自己的类名。我当前正在包含该控件的页面上的 CSS 中设置背景图像:
<style type="text/css">
.cssClass .innerDiv {
background-image: url("http://....");
}
</style>
这样就绘制了正确的图像。但如果它不存在,则不会绘制图像。
我想要的是 ServerControl 发出一些定义这些图像 url 的 CSS,这些 CSS 将被用户添加的任何 css 覆盖,并且默认 CSS 包含嵌入在 ServerControl 程序集中的图像的 URL。
我也不知道该怎么做。就此而言,我也不确定这是最好的方法。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要多种样式,请使用 CSS 类(例如 HeaderCssClass、ItemCssClass)公开各种属性。
另外,您可以检查用户是否指定了 CSS 类名,您可以使用它;否则,请使用默认值并省略控件中的自定义 CSS。
在渲染逻辑中,您可以根据用户是否指定了任何内容,将正确的 CSS 类名渲染为 DIV 的属性。所以你可以这样做:
并且仅在 HeaderCssClass 为 null 时才写出标准 CSS。
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:
And only write out your standard CSS if the HeaderCssClass is null.