views:

68

answers:

1

I'm developing a custom control which is a composition of tables and buttons. I also have a CSS that defines the styles for these. The Control is under the class 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

Also, the stylesheet is under the name styles.css and is in the same folder as the 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 got 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 more do I need to do to get this working?

Thanks

+2  A: 

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.

Canavar
Thanks. It worked.
Saurabh Manchanda
After using GetWebResourceUrl, the stylesheet doesn't get linked.
Saurabh Manchanda
@wacky_coder : Yeah I see now the problem, the first parameter, type parameter, should be the container of the resource. In this case your control's type.
Canavar
I had already tried it before even posting here. It simply didn't work. Did I miss anything? Do I need to do anything else for this method to work?
Saurabh Manchanda
The resource gets embedded into the DLL but the webpage can't find it.
Saurabh Manchanda
Issue Resolved.
Saurabh Manchanda