Embedded JavaScript Resources

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Embedded JavaScript Resources

    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:
    [code=javascript]

    function ChangeBackgroun dColor(ctrl)
    {
    ctrl.style.back groundColor = "#C00000";
    }
    [/code]

    I have an ASPX page named "Index.aspx " with the following contents:
    [code=asp]
    <%@ Page Language="vb" AutoEventWireup ="false" MasterPageFile= "~/Master.Master" CodeBehind="Ind ex.aspx.vb" Inherits="Embed dedResources.In dex"
    title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHol derID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHol derID="ContentP laceHolder1" runat="server">
    <asp:TextBox ID="CrazyTextBo x" runat="server"> </asp:TextBox>
    </asp:Content>
    [/code]

    The VB.NET Server Side code (code behind) for this page is as follows:
    [code=vbnet]
    Partial Public Class Index
    Inherits System.Web.UI.P age

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load

    End Sub

    Private Sub Index_PreRender (ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.PreRender
    Dim pathToResource As String = Page.ClientScri pt.GetWebResour ceUrl(Me.GetTyp e, "EmbeddedResour ces.ChangeTextb oxBg.js")
    Page.ClientScri pt.RegisterClie ntScriptInclude ("testResource" , pathToResource)
    'ScriptManager. RegisterClientS criptInclude(Me , Me.GetType, "testing", ResolveClientUr l(pathToResourc e))
    CrazyTextBox.At tributes.Add("o nkeypress", "ChangeBackgrou ndColor(this);" )
    End Sub
    End Class
    [/code]

    And I have a Master page named Master.Master which contains the ScriptManager for the site:
    [code=asp]
    <%@ Master Language="VB" AutoEventWireup ="false" CodeBehind="Mas ter.master.vb" Inherits="Embed dedResources.Ma ster" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitl ed Page</title>
    <asp:ContentPla ceHolder ID="head" runat="server">
    </asp:ContentPlac eHolder>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptMana ger ID="ScriptManag er1" runat="server" />
    <asp:ContentPla ceHolder ID="ContentPlac eHolder1" runat="server">

    </asp:ContentPlac eHolder>
    </div>
    </form>
    </body>
    </html>
    [/code]

    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
    [code=vbnet]
    <Assembly: WebResource("Em beddedResources .ChangeTextboxB g.js", "text/javascript")> [/code]


    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
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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:

    [code=vbnet]
    Private Sub Index_PreRender (ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.PreRender
    Dim pathToResource As String = "ChangeTextboxB g.js"
    Dim sr As New ScriptReference
    sr.Path = pathToResource
    sr.Assembly = "EmbeddedResour ces"
    sr.Name = "EmbeddedResour ces.ChangeTextb oxBg.js"
    scriptMP.Script s.Add(sr)
    CrazyTextBox.At tributes.Add("o nkeypress", "ChangeBackgrou ndColor(this);" )
    End Sub
    [/code]

    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

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      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:
      [code=vbnet]
      Private Sub Index_PreRender (ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.PreRender

      'Dim pathToResource As String = "ChangeTextboxB g.js"
      Dim pathToResource As String = "NotEmbeddedScr ipt.js"
      Dim sr As New ScriptReference
      sr.Path = pathToResource
      'sr.Assembly = "EmbeddedResour ces"
      'sr.Name = "EmbeddedResour ces.ChangeTextb oxBg.js"
      scriptMP.Script s.Add(sr)
      CrazyTextBox.At tributes.Add("o nkeypress", "ChangeBackgrou ndColor(this);" )
      End Sub
      [/code]


      The code in the JavaScript file (named "NotEmbeddedScr ipt.js") used in this example is as follows:
      [code=javascript]
      function ChangeBackgroun dColor(ctrl)
      {
      ctrl.style.back groundColor = "#0000C0";
      ctrl.style.colo r = "#FFFFFF";
      }
      [/code]

      Cheers!!

      -Frinny

      Comment

      Working...