Home
 Index > ASP.NET Web Applications > How to Embed an Image, JavaScript or other file...

ASP.NET Web Applications:  Server Controls

How to Embed an Image, JavaScript or other file in a Server Control

Using RegisterClientScriptResource and GetWebResourceUrl to embed files in your Web server control is easy (if you know all the steps you need to take to achieve this)

Added on 22 Jul 2008

Scenarios:

Scenario Summary:
Registering the files in the PreRender method has no effect.
Scenario Details:

After downloading the C# ComboBox control, I converted it to VB.NET using the conversion utility at DeveloperFusion.com. The VB.NET code seemed to compile wtihout a hitch, but running it in a web browser, it became clear that the Javascript and Image files were not being embedded in the server control.

Debugging the application didn't help as there was no error being thrown. I.e., I was able to step through the OnPreRender event method -- where the files were (presumably) being registered -- without any discernable errors.

Added on 22 Jul 2008

Solution Summary:
After searching the Web for hours, I finally found out that it's not enough just to register the files. There were some other steps to take:
Solution Details:

Step 1:  Copy the files into your project

This is obvious, but we might as well start with this.  The file needs to be in the class project's root directory, or a sub-directory of the root folder.

Step 2:   Set the file's Build Action to Embedded Resource

  1. Right click on the file in Solution Explorer and select Properties.
  2. Select Embedded Resource from the Build Action property.
  3. Select Copy always or Copy if newer from the Copy to Output Directory property

Step 3:  Build the project

Step 4:  Use Reflector to find out the Assembly reference

  1. Download Lutz Roeder's Reflector utility and run it. 
  2. Click Open and navigate to the DLL for your project
  3. Expand the node for your assembly and expand Resources
  4. Find your resource, right-click and copy the reference.

Step 5:  Modify AssemblyInfo.vb

Open your project's AssemblyInfo.vb to add a reference to your resource(s).  (If you do not see AssemblyInfo.vb, make sure that Solution Explorer is showing all files.  You might find this file in the My Project folder.

E.g.:

<Assembly: AssemblyVersion("1.0.*")> 
<Assembly: AssemblyFileVersion(
"1.0.0.0")> 
<Assembly: System.Web.UI.TagPrefix(
"SCS.WebControls""scs")> 
<Assembly: System.Web.UI.WebResource(
"ComboBox.ScsComboBoxUtil.js""text/javascript")> 
<Assembly: System.Web.UI.WebResource(
"ComboBox.DropArrow.gif""image/gif")> 

Step 6: Register the files in your PreRender method:

        Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
            Page.ClientScript.RegisterClientScriptResource(
Me.[GetType](), "ComboBox.ComboBoxUtil.js")
            
If [String].IsNullOrEmpty(Me.ImageUrl) Then
                Me.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.[GetType](), "ComboBox.DropArrow.gif")
            
End If
            MyBase.OnPreRender(e)
        
End Sub

Related Links
Lutz Roeder's Reflector Utility
Reflector is the class browser, explorer, analyzer and documentation viewer for .NET. Reflector allows to easily view, navigate, search, decompile and analyze .NET assemblies in C#, Visual Basic and IL.

Was this solution useful? Yes No Added on 22 Jul 2008
Rating: 

Copyright 2010 © E-Centric, Inc. | Terms of Use