How to make user controls know about css classes in ASP.NET

58,846

Solution 1

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.

Solution 2

Add the style on your usercontrol and import css in it.

 <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WCReportCalendar.ascx.vb"
Inherits="Intra.WCReportCalender" %>
 <style type='text/css'>    
      @import url("path of file.css");
       // This is how i used jqueryui css
      @import url("http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css");               

 </style>

 your html 
Share:
58,846
Serhat Ozgel
Author by

Serhat Ozgel

Software developer and consultant. Areas of expertise: Web application and api development, software development, cloud architecture, mobile application backend development, e-commerce software development and integration.

Updated on February 09, 2020

Comments

  • Serhat Ozgel
    Serhat Ozgel over 4 years

    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?