Ahmadreza's Notes

On .NET Framework and Software Architecture

How to embed JavaScript file in an assembly

with 2 comments

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 , ,

2 Responses

Subscribe to comments with RSS.

  1. great script, thanks for shared

    robert

    June 24, 2009 at 4:43 pm

  2. Nice post buddy………

    sam

    March 16, 2011 at 11:53 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.