Ahmadreza's Notes

On .NET Framework and Software Architecture

How to embed other resources like image and css

with 2 comments

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 = "";

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

About these ads

Written by Ahmadreza Atighechi

August 7, 2008 at 3:44 am

Posted in Blog

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. Hi, thanks for the great script..

    ricky

    June 24, 2009 at 4:36 pm

  2. thank you, very interesting script

    steve

    June 24, 2009 at 4:36 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: