473,700 Members | 2,384 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 2415
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 making many unnecessary requests for configuration files and resources files as well. I am using the...
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...
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 executed. This works great when the installer app is built with Visual Studio .NET, but it does not work...
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...
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 (...
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...
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...
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,...
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...
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...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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 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.