Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Category list

Widget Twitter not found.

Root element is missing.X

How to embed other resources like image and css

by Ahmadreza Atighechi 7. August 2008 03:44

 

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" 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 = "<link rel='stylesheet' text='text/css' href='{0}' />";

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

 

kick it on DotNetKicks.com

Tags: , ,

Blog

The information on this web site is provided "AS IS" with no warranties, and confers no rights.

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen