Click here to Skip to main content
6,305,776 members and growing! (17,235 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

How to Embed /Access JavaScript, CSS, Images in an Assembly.

By Bhupendra Sinha

This article will explain how to embed/access JavaScript, CSS and images to server control’s assembly.
C# 2.0, Windows, .NET 2.0, ASP.NET, VS2005, Dev
Posted:30 Nov 2007
Updated:14 Sep 2008
Views:17,801
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
12 votes for this article.
Popularity: 3.38 Rating: 3.13 out of 5
4 votes, 33.3%
1
2 votes, 16.7%
2
1 vote, 8.3%
3
1 vote, 8.3%
4
4 votes, 33.3%
5

Introduction

This article makes you go through the concept on how to embed/access JavaScript, CSS and images to server control's assembly.

Background

Resources embedded within an assembly can be accessed through the WebResource.axd HTTP Handler. This Handler is needed because we need some mechanism to get this code at the client side to be executed and rendered as well (see the source code of the page - you will observe some strange URL code that you haven't written anywhere though it is present on that page. This is because of embedding and accessing JavaScript/CSS/Images through WebResource.axd).

Using the Code

We need to follow few steps for the completion of Embedding JS/CSS/Images in an Assembly.

Step 0

Simply add the file (JS/CSS/Image) to your project, go to the Properties pane, and set the Build Action to Embedded Resource.

Step 1: AssemblyInfo.cs Entries

[assembly: System.Web.UI.WebResource("Img_Button.gif", "img/gif")];//see Note

[assembly: System.Web.UI.WebResource("TextBox.css", "text/css")];
[assembly: System.Web.UI.WebResource("ABC.js", "text/js")];

Step 2: Embedding an Image

Image img= new Image();
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Img_Button.gif");

Step 3: Embedding a Stylesheet

string tempLink= "<link rel='stylesheet' text='text/css' href='{0}' />";
string location = Page.ClientScript.GetWebResourceUrl(this.GetType(), "TextBox.css");
LiteralControl include = new LiteralControl(String.Format(tempLink, location));
((HtmlControls.HtmlHead) Page.Header).Controls.Add(include);

Step 4: Embedding JavaScript

string srptLoc =Page.ClientScript.GetWebResourceUrl(this.GetType(), "ABC.js");
Page.ClientScript.RegisterClientScriptInclude("ABC.js", srptLoc);

Note

  1. Add .axd extension in your virtual directory configuration (all configuration settings should be like that of ASPX extension).
  2. File name should be DefaultNamespace.Filename.Extension (use reflector tool to get the actual name in the embedded assembly). This is required in Assemblyinfo.cs file entry see Step-1.
  3. Write all the above code for registration OnPrerender(or OnInit) event.
//For Example :

protected override void OnPreRender(EventArgs e)
{
    // When pre-rendering, add in external JavaScript file
    Page.ClientScript.RegisterClientScriptInclude("ABC", 
                 Page.ClientScript.GetWebResourceUrl(this.GetType(), 
                                             "ABC.js"));

    base.OnPreRender(e);
}

You may have possibly liked this article. I have written this article with the hope that it may somehow help you in due course of your learning.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Bhupendra Sinha


Member
Having Experience of more than 4years,ASP.NET(1.1 and 2.0),c#.net,SharePoint, Webservices,XML, SQL Server 2000, VS 2005/2003, Remoting, AD, Nunit, NHibernate,Design Patterns,Microsoft Ajax,Agile Technology.
Occupation: Software Developer (Senior)
Company: Vertex Software
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralShort n sweet Pinmemberksalvage0:35 7 Jul '09  
GeneralDebugging? PinmemberFlash Heart3:52 19 Feb '09  
GeneralRe: Debugging? PinmemberFlash Heart4:03 19 Feb '09  
Generalsource code Pinmemberzhaojicheng22:10 13 Dec '07  
GeneralRe: source code PinmemberBhupendra Sinha19:44 16 Dec '07  
GeneralInteresting article Pinmembersambuccus11:19 9 Dec '07  
GeneralRe: Interesting article [modified] PinmemberBhupendra Sinha21:10 13 Dec '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Sep 2008
Editor: Deeksha Shenoy
Copyright 2007 by Bhupendra Sinha
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project