Walkthrough: Embedding a JavaScript File as a Resource in an Assembly
In this walkthrough, you will include a JavaScript file as an embedded resource in an assembly. You embed a JavaScript file when you have a client-script component that must be distributed with an assembly that you have created. For example, you might create a custom ASP.NET server control that uses JavaScript files to implement AJAX functionality for ASP.NET. You can embed the JavaScript files in the assembly, and they can then be referenced from a Web application that registers the assembly.
To begin, you will create a file that contains the JavaScript code that you want to embed in the assembly.
To embed a client script file in an assembly
In Visual Studio, create a new class library project named SampleControl.
Add references to the System.Web, System.Drawing, and System.Web.Extensions assemblies to the project.
Add a new JScript file named UpdatePanelAnimation.js to the project.
Add the following code to the UpdatePanelAnimation.js file:
The code contains a JavaScript function that temporarily displays a colored border around an UpdatePanel control.
In the Properties window for the UpdatePanelAnimation.js file, set Build Action to Embedded Resource.

Add a class file named CustomControl to the project.
Replace any code in the CustomControl file with the following code:
This class contains properties for customizing the border that is displayed around the UpdatePanel control. The code also registers JavaScript code to use in a Web page. The code creates a handler for the load event of the Sys.Application object. The animate function in the UpdatePanelAnimation.js file is called when a partial-page update is processed.
Add the following line to the AssemblyInfo file.
Note:The AssemblyInfo.vb file is in the My Project node of Solution Explorer. If you do not see any files in the My Project node, in the Project menu, click Show All Files. The AssemblyInfo.cs file is in the Properties node of Solution Explorer.
The WebResource definition must include the default namespace of the assembly and the name of the .js file.
Build the project.
When compilation finishes, you will have an assembly named SampleControl.dll. The JavaScript code in the UpdatePanelAnimation.js file is embedded in this assembly as a resource.
You can now reference the embedded script file in a Web application.
Note: |
|---|
Although you can create the class library project and the Web site in the same Visual Studio solution, in this walkthrough it is not assumed that you are doing this. Having the projects in separate solutions emulates how a control developer and a page developer would work separately. However, for convenience you can create both projects in the same solution and make small adjustments to procedures in the walkthrough. |
To reference the embedded script file
In Visual Studio, create a new AJAX-enabled Web site.
Create a Bin folder in the root directory of the Web site.
Copy SampleControl.dll from the Bin\Debug or Bin\Release directory of the class library project to the Bin folder of the Web site.
Note:If you created the class library project and the Web site in the same Visual Studio solution, you can add a reference from the class library project to the Web site. For details, see How to: Add a Reference to a Visual Studio Project in a Web Site.
Replace the code in the Default.aspx file with the following code:
This code includes a ScriptReference control that references the assembly and the name of the .js file that you created in the previous procedure. The name of the .js file includes a prefix that references the default namespace of the assembly.
Run the project, and in the page, click dates in the calendar.
Every time that you click a date in the calendar, you see a green border around the UpdatePanel control.
This walkthrough showed you how to embed a JavaScript file as a resource in an assembly. The embedded script file can be accessed in a Web application that contains the assembly.
The next step is to learn how to embed localized resources in an assembly for use in client script. For more information, see Walkthrough: Embedding Localized Resources for a JavaScript File.