Ahmadreza's Notes

On Software Development

Posts Tagged ‘VS 2005

How to embed other resources like image and css

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

Advertisements

Written by Ahmadreza Atighechi

August 7, 2008 at 3:44 am

Posted in Blog

Tagged with , ,

How to embed JavaScript file in an assembly

Developing ASP.NET web controls is always interlocked with creating javascript codes in order to create client side functions. Moreover, a developer always has a tendency to release minimum files, most of the time an assembly (DLL) which includes all requirements for web control, is best case. This post is an attempt to represent how to embed JS file in an assembly.

First of all you have to create an ASP.Net AJAX-Enabled Web Site  named "EmbededJSTest" and add System.Web.Extensions references to project . In order to add this assembly you have to install  ASP.NET AJAX 1.0. Add another Web control library project to solution named "EmbededJSControl". Add project reference of "EmbededJSControl" to "EmbededJSTest".

Create a JavaScript File to project named "JSResource.js". Open js file and write code:

function HelloWorld()

{

alert(‘Hello World!’);

}

Save and close this file

In property page of this file change the value of "Build Action" Property to "Embedded Resource".

Open AssemblyInfo.cs  at the end of file add following line

[assembly: System.Web.UI.WebResource("EmbededJSControl.JSResource.js", "application/x-javascript")]

Open Default.aspx file in source mode. Change ScriptManager Control to following:

<asp:ScriptManager ID="ScriptManager1" runat="server" >

<Scripts>

<asp:ScriptReference Assembly="EmbededJSControl" Name="EmbededJSControl.JSResource.js" />

</Scripts>

</asp:ScriptManager>

Add a HTML button :

<input id="Button1" type="button" value="Hello" onclick="HelloWorld()" />

Save file and close this file.

Execute project and click on Hello button.

By applying this method in your Web Control projects there is no need to release js file with assembly dll’s.

You can also download source sample here

Written by Ahmadreza Atighechi

July 22, 2008 at 5:10 am

Posted in Blog

Tagged with , ,