How To Embedded StyleSheet File with Custom Control.

How to embedded StyleSheet file with custom control? Question has been opened many time on ASP.NET forum.

So in this article, I will try to demonstrate how to achieve this task.

In General

In general, if you want to embedded any thing with custom control, you need first to make its "Build Action" property to be "Embedded Resource", and then you need to add to the custom control assemblies using the <Assembly> attribute typically above class name, and finally you need to register in the page so it can be rendered.

I will illustrate all these things in details through the article.

 

Step One: make it as Embedded Resource

 After you added the StyleSheet file to your Custom Control Class Library project, first thing you need to do is to make build action property for the stylesheet file as embedded resource, otherwise your page will not be able to see the stylesheet file.

To do this, from the VS do a right click on the stylesheet file, and click on Properties,  and then change the "Build Action" property to be "Embedded Resource".

 

Step Two: Add it to the Custom Controls Assemblies

 Second thing you need to do is to register the Css file to the custom control assemblies by adding the below code typically above class name


[assembly: WebResource("YourProjectName.StyleSheetFileName.css", "text/css")]

namespace YourNameSpace

{

   public class MyCustomControl : CompositeControl

    {

       //Custom Control Rest Code

 

 Step Three: Register Css File on the Page

Finally, we have to tell the Page that our custom control has Css file needed to be registered when the final HTML is generated for that page. So we need to write a bit of code in the OnInit method.

 

protected override void OnInit(EventArgs e)

{

    base.OnInit(e);

   

    string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(),

    "YourProjectName.CssFileName.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";

   

               

    Page.ClientScript.RegisterClientScriptBlock(this.GetType, "cssFile", css, false);

}

 

And that's it, you can now use the CSS StyleSheet file within your Custom Control.

 

I hope some one will found this very helpful.

 

~ Abdulla AbdelHaq

 

Published Saturday, September 26, 2009 1:52 PM by Abdulla.Abdelhaq

Comments

# re: How To Embedded StyleSheet File with Custom Control.

Monday, September 28, 2009 9:08 AM by sdowding

this.GetType() won't work if someone subclasses your control in a different assembly. Using typeof(MyCustomControl) will ensure the correct assembly is used. I learned the hard way :(

# re: How To Embedded StyleSheet File with Custom Control.

Thursday, November 12, 2009 10:15 AM by Mike

Is there any workaround if in the css i have used background images.then what should be the paths for those images

# re: How To Embedded StyleSheet File with Custom Control.

Tuesday, November 24, 2009 9:21 AM by amer abu ein

my respects!

# re: How To Embedded StyleSheet File with Custom Control.

Saturday, January 02, 2010 12:06 PM by Valentino

Thanks

# re: How To Embedded StyleSheet File with Custom Control.

Tuesday, January 05, 2010 10:14 AM by Paul Diggle

This really helped me. Thanks.

# re: How To Embedded StyleSheet File with Custom Control.

Thursday, February 25, 2010 8:33 AM by Acinnina

а все таки: бесподобно!  а82ч

# re: How To Embedded StyleSheet File with Custom Control.

Wednesday, February 02, 2011 1:06 PM by mhoty

<a href=http://www.forumpostbacklinks.com>forum posting service</a>

# re: How To Embedded StyleSheet File with Custom Control.

Sunday, February 13, 2011 11:28 AM by Strannik

Thanks a lot!

# re: How To Embedded StyleSheet File with Custom Control.

Wednesday, April 27, 2011 3:49 AM by best makeup brushes

tnx, that help a lot

# re: How To Embedded StyleSheet File with Custom Control.

Thursday, May 05, 2011 1:50 PM by best-shampooforhair.com

Hello,

nice work! Does this work in every .NET framework version?

# re: How To Embedded StyleSheet File with Custom Control.

Wednesday, June 29, 2011 10:07 PM by jianyun

It is greate but the css in the body instead of header

# re: How To Embedded StyleSheet File with Custom Control.

Sunday, November 27, 2011 3:21 PM by health beauty

Very nice article and straight to the point. I don’t know if this is in fact the best place to ask but do you folks have any ideea where to employ some professional writers? Thanks in advance :)

# re: How To Embedded StyleSheet File with Custom Control.

Thursday, January 19, 2012 6:07 AM by Eyüp Gürel

Great article. Congratulations.

Leave a Comment

(required) 
(required) 
(optional)
(required)