Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 785
Serhat Ozgel Avatar asked Aug 29 '08 11:08

Serhat Ozgel


People also ask

How do I know my CSS class?

If you're using the Chrome inspector (right click page, inspect element), highlight the line and look to the right. It should show the CSS class and any rules that are used, and a link to the CSS file.

What are the ways through which controls in asp net can be applied with styles?

The best way to style a control is to use the style properties defined by the control and to use a style sheet or inline styles to make minor adjustments to individual elements if necessary. To override a style defined by a control's style properties, use the ! important CSS rule in a style sheet or inline styles.

Can we use CSS in asp net?

Yes, you have to link the style sheet file (. css) by adding the link tag. You can also simply drag the css file into the html section of the . aspx code right under head tag, that will work too - that will create the link for you.

How do you implement a class in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)


2 Answers

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.

like image 163
Adam Lassek Avatar answered Sep 22 '22 15:09

Adam Lassek


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  
like image 43
Abdul Saboor Avatar answered Sep 21 '22 15:09

Abdul Saboor