Associating CSS Stylesheets with ASP.NET User Management

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

The CompositeControl control type is in the MyControls namespace, and the class definition is in the CompositeControl.cs class file and the dll created file is called MyControls.dll

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

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

When I add this control to my ASP.NET web page and check the HTML source when running on the local host, I see that all the control tags have the class attribute correctly set, but the source does not include the <link> , which is necessary to include external style sheet.

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

+7
css stylesheet custom-controls
source share
1 answer

External source files are not automatically included in the page. You must register them manually in your control on 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 using ClientScriptManager.GetWebResourceUrl to get the location of the embedded resource. Here is an example of a link to an embedded resource.

+7
source share

All Articles