Click here to Skip to main content
13,250,182 members (55,384 online)
Click here to Skip to main content
Add your own
alternative version

Stats

60.3K views
707 downloads
26 bookmarked
Posted 30 Nov 2007

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

, 14 Sep 2008
Rate this:
Please Sign up or sign in to vote.
This article will explain how to embed/access JavaScript, CSS and images to server control’s assembly.

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)

Share

About the Author

Bhupendra Sinha
Team Leader Vertex Software
India India
5+years,ASP.NET(1.1 and 2.0,3.5),c#.net,SharePoint, Webservices,XML, SQL Server 2000, VS 2008/2005/2003, Remoting, AD, Nunit, NHibernate,Design Patterns,Microsoft Ajax,Agile Technology,LINQ,WCF,WF,REST Service,Microsoft MVC framework

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
HalDiggs30-Jul-12 10:52
memberHalDiggs30-Jul-12 10:52 
GeneralShort n sweet Pin
ksalvage7-Jul-09 0:35
memberksalvage7-Jul-09 0:35 
GeneralRe: Short n sweet Pin
Bhupendra Sinha11-Jul-09 9:39
memberBhupendra Sinha11-Jul-09 9:39 
QuestionDebugging? Pin
Flash Heart19-Feb-09 3:52
memberFlash Heart19-Feb-09 3:52 
AnswerRe: Debugging? Pin
Flash Heart19-Feb-09 4:03
memberFlash Heart19-Feb-09 4:03 
GeneralRe: Debugging? Pin
Bhupendra Sinha11-Jul-09 9:40
memberBhupendra Sinha11-Jul-09 9:40 
Generalsource code Pin
zhaojicheng13-Dec-07 22:10
memberzhaojicheng13-Dec-07 22:10 
GeneralRe: source code Pin
Bhupendra Sinha16-Dec-07 19:44
memberBhupendra Sinha16-Dec-07 19:44 
GeneralInteresting article Pin
sambuccus9-Dec-07 11:19
membersambuccus9-Dec-07 11:19 
GeneralRe: Interesting article [modified] Pin
Bhupendra Sinha13-Dec-07 21:10
memberBhupendra Sinha13-Dec-07 21:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 14 Sep 2008
Article Copyright 2007 by Bhupendra Sinha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid