473,550 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedded JavaScript Resources

9,735 Recognized Expert Moderator Expert
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 "EmbeddedResour ces" with the namespace "EmbeddedResour ces"

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 "ChangeTextboxB g.js" located in the root directory of my project with the following contents:
Expand|Select|Wrap|Line Numbers
  2. function ChangeBackgroundColor(ctrl)
  3. {
  4.     ctrl.style.backgroundColor = "#C00000";
  5. }
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>
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
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.     End Sub
  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
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" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  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">
  17.         </asp:ContentPlaceHolder>
  18.     </div>
  19.     </form>
  20. </body>
  21. </html>
I have marked the embedded resource (changeTextboxB g.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 "ChangeTextboxB g.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
Apr 3 '08 #1
2 2404
9,735 Recognized Expert Moderator Expert

I finally got my Embedded JavaScript Resource to work in the page.
I added a ScriptManagerPr oxy 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
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

Apr 3 '08 #2
9,735 Recognized Expert Moderator Expert
Just a little bit more information...i f 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.

Expand|Select|Wrap|Line Numbers
  1.     Private Sub Index_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  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

The code in the JavaScript file (named "NotEmbeddedScr ipt.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. }

Apr 3 '08 #3

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

Similar topics

by: Philipp Seidel | last post by:
Hi there! I did not know, where this topic fits in, so I put it here. I have a minor problem with embedded resources, which puzzles me quite a lot. I'd be glad if anyone can help me on this: I've added a few files (images) to an assembly as embedded resources. Within the project folder these files are in a subfolder named "images".
by: Drew | last post by:
Assembly asm = Assembly.GetExecutingAssembly(); me = new Bitmap(asm.GetManifestResourceStream("me.gif")); I have used this before without any problem, but now I get: An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll Additional information: 'null' is not a valid value for 'stream'.
by: Chris Schremser | last post by:
I have a question regarding embedded controls in IE. We are looking to replace a small VB ActiveX control with something written in C#. We have written the control and it instantiates correctly in IE using the <object> tag. My question is this. When the browser visits the pages and requests the object from the server, it appears to be...
by: Kyle Kaitan | last post by:
I have an assembly (AppResources.dll) which contains a number of embedded resource files. Most of these are key/value pairs of relevant strings; a few are images and sounds; some more are XML files. My application will load the resources into memory as they are needed. I would like to be able to read and write to these embedded resources. ...
by: Wysiwyg | last post by:
Is there any way to add an embedded resource to a project without copying it to the project's directory? I have shared resources and don't want each project using the images, xml files, etc. to need to be updated with the current copy before being built. I also don't want projects being built with the old copy. Thanks! Bill
by: Jon Rista | last post by:
I have a project where I need to create a windows .exe by compiling code and linking in some resources. This program thats being generated is somewhat unconventional, and I'll explain how. I'm generating a very simple installer app that embeds referenced .dll files inside it, which are extracted and referenced when the installer app is...
by: Johann Blake | last post by:
I'm having trouble grasping how ASP.NET correctly locates resources. There is plenty of documentation on this subject but some things are not clear at all. In my ASP.NET application, I have multiple web forms. When you compile the application, it creates a DLL. If you view this DLL using ILDASM (the manifest), it shows info about the...
by: ChristianD | last post by:
for some reason i can't access a resource (.js) if the control files and the resource are in a subfolder off the project root or just the resource is in a subfolder. the code is as follows ( this is an example with the resourse in a sub-folder called Resources): private void RegisterScript(Control targetControl) { String pageScriptName =...
by: Bob Rock | last post by:
Hello, I'd like to programmatically access keys and values of on .resources file embedded in my assembly. I've seen that the assembly class provides the GetManifestResourceStream method .... but then I don't know I to access single keys and values inside the same .resources file. I'd like to be able to do something like what is possible...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.