David McWee's Blog  
MicroLink Blogs MicroLink, LLC   About Us | Solutions | Our Products | Case Studies | Clients | Careers

Meet
David McWee


LinkedIn

Subscribe

Feed Your Reader
- or -
Subscribe by Email

Archives

October 2007 (2)
November 2007 (3)
December 2007 (2)
January 2008 (1)
May 2008 (7)
April 2008 (4)
March 2008 (2)
June 2008 (2)
July 2008 (2)
August 2008 (1)
September 2008 (4)
October 2008 (1)
November 2008 (2)
December 2008 (5)
January 2009 (8)
February 2009 (6)
March 2009 (3)
April 2009 (2)
May 2009 (3)
June 2009 (3)

Links

Photos
Archive
Archive (Calendar)
STSDev 2008 (CodePlex)
StsDev (CodePlex)
SharePoint Team Blog
Daniel Larson
Mark Wagner
The Kid
Ted Pattison
David Mann
BizTalk Server Team Blog
ECTS Extension (Codeplex)

Tag Cloud

.NET, Azure, ClientScriptManager, ECTS, IDV, JavaScript, Linq, Microsoft, MOSS, SharePoint, Silverlight, SPFeatureReceiver, SPItemEventReceiver, StsDev, STSDev 2008, Twitter, Virtual Earth, Visual Studio 2008, Visual Studio TFS, Windows 2003, Windows 7, Workflow,

Web Resources, Solving Javascript inclusion in your web part 

Tags: .NET, ClientScriptManager, JavaScript, MOSS

Working with client side controls requires a lot of javascript, and I have often just written the javascript in the Render or RenderHTML functions of my web controls.  This works, but is NOT the best, or even a recommended method to do this.  So I began looking around and found this nice capability, but there are some major pitfalls to be aware of!

 This example is based on a current Mirolink development project for Virtual Earth 6.  The projects namespace is MicroLink.Applications.VirtualEarth.Core.  The javascript file is at the root level of the project and is Core.js.  This information is critical to be aware of in the steps below!

 ASP.NET 2.0 introducted the WebResource capability to include javascript, image, and other "resource" files within a DLL.  Using the WebResource is actually pretty simple, just follow these three steps.

Step 1

Add your resource (.js, .css, etc) to the project you want to bundle it with.  Set the Build Action property to Embedded Resource.

Step 2

In the AssemblyInfo.cs file, usually located in the Properties folder add the following line:

[assembly: WebResource("MicroLink.Applications.VirtualEarth.Core.Core.js", "application/x-javascript")]

***NOTE***
Notice that the resource name is actually formed [Package] {.[SubFolder}.[Filename].  This is not noted anywhere in the MSDN!

Step 3
In the class file add the following line for security

[AspNetHostingPermission(SecurityAction.Demand, Level= AspNetHostingPermissionLevel.Minimal)]

Finally to reference the resource:

string coreJS = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MicroLink.Applications.VirtualEarth.Core.Core.js");

 
Posted by David McWee on 9-Nov-07
8841 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
 
Name:
URL:
Email:
Comments: