By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Embedded JavaScript Resources

Frinavale
Expert Mod 5K+
P: 9,731
I am attempting to use embedded resources in an Ajax Enabled ASP.NET Web Application. I'm using Visual Studio 2008 and VB.NET server side code.

The project is called "EmbeddedResources" with the namespace "EmbeddedResources"

My resources are not included when the content is rendered in the browser.
I have marked my JavaScript resource as Embedded. I am able to retrieve the path to the resource from the WebResource.axd HTTP Handler during run time; however that resource can never be found by the browser during run time.

I have an embedded JavaScript file named "ChangeTextboxBg.js" located in the root directory of my project with the following contents:
Expand|Select|Wrap|Line Numbers
  1.  
  2. function ChangeBackgroundColor(ctrl)
  3. {
  4.     ctrl.style.backgroundColor = "#C00000";
  5. }
  6.  
I have an ASPX page named "Index.aspx" with the following contents:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Master.Master" CodeBehind="Index.aspx.vb" Inherits="EmbeddedResources.Index" 
  2.     title="Untitled Page" %>
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  6. <asp:TextBox ID="CrazyTextBox" runat="server"></asp:TextBox>
  7. </asp:Content>
  8.  
The VB.NET Server Side code (code behind) for this page is as follows:
Expand|Select|Wrap|Line Numbers
  1. Partial Public Class Index
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6.     End Sub
  7.  
  8.     Private Sub Index_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  9.         Dim pathToResource As String = Page.ClientScript.GetWebResourceUrl(Me.GetType, "EmbeddedResources.ChangeTextboxBg.js")
  10.         Page.ClientScript.RegisterClientScriptInclude("testResource", pathToResource)
  11.         'ScriptManager.RegisterClientScriptInclude(Me, Me.GetType, "testing", ResolveClientUrl(pathToResource))
  12.         CrazyTextBox.Attributes.Add("onkeypress", "ChangeBackgroundColor(this);")
  13.     End Sub
  14. End Class
  15.  
And I have a Master page named Master.Master which contains the ScriptManager for the site:
Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Master.master.vb" Inherits="EmbeddedResources.Master" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8.     <asp:ContentPlaceHolder ID="head" runat="server">
  9.     </asp:ContentPlaceHolder>
  10. </head>
  11. <body>
  12.     <form id="form1" runat="server">
  13.     <div>
  14.         <asp:ScriptManager ID="ScriptManager1" runat="server" />
  15.         <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  16.  
  17.         </asp:ContentPlaceHolder>
  18.     </div>
  19.     </form>
  20. </body>
  21. </html>
  22.  
I have marked the embedded resource (changeTextboxBg.js) as being accessible through the WebResource.axd HTTP Handler by adding the following line to my AssemblyInfo.vb
Expand|Select|Wrap|Line Numbers
  1. <Assembly: WebResource("EmbeddedResources.ChangeTextboxBg.js", "text/javascript")> 

As you can see I attempted using the ScriptManager to include the resource to the page, but this made no difference.

I've also attempted change the properties on the "ChangeTextboxBg.js" embedded file so that it copies itself to the output directory...but this also made no difference.

I am completely baffled as to why this doesn't work.
If anyone has any idea of what the problem may be I'd be happy to hear your thoughts!

Thanks a lot
-Frinny
Apr 3 '08 #1
Share this Question
Share on Google+
2 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Ok!

I finally got my Embedded JavaScript Resource to work in the page.
I added a ScriptManagerProxy called "scriptMP" to my Index.aspx page and added my script to that in my PreRender event:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Index_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.         Dim pathToResource As String = "ChangeTextboxBg.js"
  3.         Dim sr As New ScriptReference
  4.         sr.Path = pathToResource
  5.         sr.Assembly = "EmbeddedResources"
  6.         sr.Name = "EmbeddedResources.ChangeTextboxBg.js"
  7.         scriptMP.Scripts.Add(sr)
  8.         CrazyTextBox.Attributes.Add("onkeypress", "ChangeBackgroundColor(this);")
  9.     End Sub
  10.  
Note the that I set the ScriptReference Name property to include the Assembly Name that the Embedded JavaScript Resource is a part of (line 6 in the above code). This is important, otherwise it wont find the resource.

Now I get to apply this stuff to my real project :D :D

-Frinny
Apr 3 '08 #2

Frinavale
Expert Mod 5K+
P: 9,731
Just a little bit more information...if you are not using an embedded resource with you do not set the Assembly and do not set the Name Properties of the ScriptReference that you are adding to the ScriptProxy.

eg:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Index_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  2.  
  3.         'Dim pathToResource As String = "ChangeTextboxBg.js"
  4.         Dim pathToResource As String = "NotEmbeddedScript.js"
  5.         Dim sr As New ScriptReference
  6.         sr.Path = pathToResource
  7.         'sr.Assembly = "EmbeddedResources"
  8.         'sr.Name = "EmbeddedResources.ChangeTextboxBg.js"
  9.         scriptMP.Scripts.Add(sr)
  10.         CrazyTextBox.Attributes.Add("onkeypress", "ChangeBackgroundColor(this);")
  11.     End Sub
  12.  

The code in the JavaScript file (named "NotEmbeddedScript.js") used in this example is as follows:
Expand|Select|Wrap|Line Numbers
  1. function ChangeBackgroundColor(ctrl)
  2. {
  3.         ctrl.style.backgroundColor = "#0000C0";
  4.         ctrl.style.color = "#FFFFFF";
  5. }
  6.  
Cheers!!

-Frinny
Apr 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.