![]() |
Languages »
C# »
General
Intermediate
License: The Code Project Open License (CPOL)
How to Embed /Access JavaScript, CSS, Images in an Assembly.By Bhupendra SinhaThis 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article makes you go through the concept on how to embed/access JavaScript, CSS and images to server control's assembly.
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).
We need to follow few steps for the completion of Embedding JS/CSS/Images in an Assembly.
Simply add the file (JS/CSS/Image) to your project, go to the Properties pane, and set the Build Action to Embedded Resource.
[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")];
Image img= new Image();
img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Img_Button.gif");
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);
string srptLoc =Page.ClientScript.GetWebResourceUrl(this.GetType(), "ABC.js");
Page.ClientScript.RegisterClientScriptInclude("ABC.js", srptLoc);
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. 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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 Web16 | Advertise on the Code Project |