How to embed other resources like image and css

Blog comments edit

Embedding other resources like Cascade style sheets and images is possible as well as JavaScript file. I wrote "[How to embed JavaScript file in an assembly](How-to-embed-JavaScript-file-in-an-assembly.aspx)" in previous post. For embedding images you have to do same way with minor changes in accessing resource. You should add you file into your project and

change the value of "Build Action" property to "Embedded Resource".  In Assembly info file you should add following line for images into AssemblyInfo.cs file:

 

[assembly: System.Web.UI.WebResource("EmbededJSControl.Logo.GIF", "img/gif")]

 

And following line for css file:

 

[assembly: System.Web.UI.WebResource("EmbededJSControl.Stylesheet.css", "text/css")]

 

To use image resource in your pages you should add following code. In case you use resource in same project you could use this.GetType() as first parameter otherwise you should create an instance of control from project in which you included resource and get type of that control.

 

Image img = new Image();

img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "EmbededJSControl.Logo.GIF");

For including stylesheet use following code:

 

string cssLink = "";

string location = Page.ClientScript.GetWebResourceUrl(this.GetType(), "EmbededJSControl.Stylesheet.css");

LiteralControl css = new LiteralControl(String.Format(cssLink, location));

Page.Header.Controls.Add(css);

 

Complete sample code is available [here](http://gkasoq.bay.livefilestore.com/y1peTj3VAMB0CgQFtkrQCdRlXxnjvPJ5t6Pzku8YfWyuUSFx9bGusFmlFhOIFBGBtVtjShgUOybQp1UYCkvcTEkjw/EmbededJSSource2.zip?download&psid=1) ![kick it on DotNetKicks.com](http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fahmadreza.com%2fgf%2fblog%2fhow-to-embed-other-resources-like-image-and-css%2f)

Comments