Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking a CSS Stylesheet with an ASP.NET Custom Control

I'm developing a custom control which is a composition of tables and buttons. I also have an external CSS stylesheet that defines the styles for these elements.

The Control's type is CompositeControl, under namespace MyControls and the definition of the class is in a class file CompositeControl.cs and the dll file generated is named MyControls.dll

The stylesheet is called styles.css and is in the same folder as CompositeControl.cs

For each control (Button, TableCell, etc.), I have specified the CssClass property.

When I add this control to my ASP.NET webpage and check the HTML source when run at localhost, I see all the control tags have the class attribute correctly set, but the source doesn't include the <link> tag which is necessary for including an external stylesheet.

Could someone tell me what else I need to do to make this work? Thanks.

like image 315
Saurabh Manchanda Avatar asked Jun 04 '10 20:06

Saurabh Manchanda


People also ask

Can you use CSS with ASP NET?

ASP.NET Web pages function as HTML pages at run time. You can therefore use cascading style sheets (CSS) to set the appearance of any elements on the page other than Web server controls.

What is CSS ASP Net?

CSS (Cascading Style Sheets) is used for defining styles to display the elements written in a markup language. It helps user separate the HTML or XHTML content from it style.


1 Answers

External source files are not included to the page automatically. You should register them manually within your control to your page :

HtmlLink cssSource = new HtmlLink();
cssSource.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), "styles.css");
cssSource.Attributes["rel"] = "stylesheet";
cssSource.Attributes["type"] = "text/css";
Page.Header.Controls.Add(cssSource);

Also try to use ClientScriptManager.GetWebResourceUrl to get location of your embedded resource. Here is an example of referencing an embedded resource.

like image 141
Canavar Avatar answered Sep 28 '22 10:09

Canavar