Ahmadreza's Notes

On Software Development

Posts Tagged ‘Ajax

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

Advertisements

Written by Ahmadreza Atighechi

July 22, 2008 at 5:10 am

Posted in Blog

Tagged with , ,