views:

11284

answers:

4

Since there are no header sections for user controls in asp.net, user controls have no way of knowing about stylesheet files. So css classes in the user controls are not recognized by visual studio and produces warnings. How can I make a user control know that it will relate to a css class, so if it is warning me about a non-existing css class, it means that the class really do not exist?

Edit: Or should I go for a different design like exposing css classes as properties like "HeaderStyle-CssClass" of GridView?

A: 

If you are creating composite UserControl, then you can set the CSSClass property on the child controls..

If not, then you need to expose properties that are either of the Style type, or (as I often do) string properties that apply CSS at the render type (i.e. take them properties and add a style attribute to the HTML tags when rendering).

Rob Cooper
+22  A: 

Here's what I did:

<link rel="Stylesheet" type="text/css" href="Stylesheet.css" id="style" runat="server" visible="false" />

It fools Visual Studio into thinking you've added a stylesheet to the page but it doesn't get rendered.


Here's an even more concise way to do this with multiple references;

<% if (false) { %>
    <link rel="Stylesheet" type="text/css" href="Stylesheet.css" />
    <script type="text/javascript" src="js/jquery-1.2.6.js" />
<% } %>

As seen in this blog post from Phil Haack.

Adam Lassek
Thanks a lot. It worked for me.
Cyril Gupta
`+1` Clever solution!
sshow
A: 

@buyutec

No, it's not valid that's why it's visible="false" ;) But it does fool VS into recognizing your stylesheet. The problem is, CSS support in VS is just advanced enough to be really annoying. It's supposed to validate your css classes by your stylesheet, but it doesn't take things like user controls into account.

Adam Lassek
A: 

http://weblogs.asp.net/scottgu/archive/2007/07/25/vs-2008-web-designer-and-css-support.aspx have a look at Scott Gu's post on the 2008 features shows the way. You can always remove the css link from your user controls and just leave it your master page once you've sorted your layouts.

dom