473,785 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedded JavaScript Resources

Frinavale
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
  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 (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
-Frinny
Apr 3 '08 #1
2 2419
Frinavale
9,735 Recognized Expert Moderator Expert
Ok!

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
  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
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.

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 "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. }
  6.  
Cheers!!

-Frinny
Apr 3 '08 #3

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

Similar topics

0
761
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".
5
8231
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'.
0
1676
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 making many unnecessary requests for configuration files and resources files as well. I am using the...
2
1895
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. Is it possible to write to an embedded resource within an assembly? If so, how? If it's not...
7
5430
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
4
5893
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 executed. This works great when the installer app is built with Visual Studio .NET, but it does not work...
0
2199
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 resources stored in the DLL. There is essentially an embedded resource "section" for each web form. The...
0
1584
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 = "Cbs.Ui.Controls.Resources.ControlScript"; if (...
4
1739
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 with the ResourceManager class but I haven't found a way to use it with embedded resources but only...
0
9484
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.