locked
"Page.ClientScript.GetWebResourceUrl" does not work RRS feed

  • Question

  • User-649410219 posted

    Hi all,

    I'm trying to use an Embedded Resource Image in my custom Control, but it does not work.

    Steps I done:

    1) Create an Folder inside the Project (Resources);
    2) Add the Image "test.gif";
    3) Change the attribute "Build Action" to "Embedded Resource" on File Property Tab;
    4) Add, on AssemblyInfo.vb: <Assembly: WebResource("<My Namespace>.Resources.test.gif", "image/gif", PerformSubstitution:=True)>;
    5) On CreateChildControl I add: 

    (...)
    Dim img as new Image
    img.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(Me.GetType(), "<My Namespace>.Resources.test.gif")
    (...)
    Me.controls.add(img)

    I already tried another stuffs, clean my browser cache and other suggestions. The <IMG ... /> tag appears on page source, but the image does not appear.

    What I'm doing wrong?

    Thanks! 

    Friday, February 29, 2008 1:26 PM

Answers

  • User-16411453 posted

    working code

    working code

     

     

     

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls


    <DefaultProperty("Text"), ToolboxData("<{0}:Test runat=server></{0}:Test>")> _
    Public Class Test
    Inherits WebControl

    Protected Overrides Sub CreateChildControls()

    Dim cs As ClientScriptManager = Me.Page.ClientScript
    Dim rsType As Type = Me.GetType()

    Dim img As New ImageButton
    img.ImageUrl = cs.GetWebResourceUrl(rsType, "ICE5Software.teamRoster.PlayAudioButton.gif")
    Me.Controls.Add(img)

    MyBase.CreateChildControls()

    End Sub

    End Class
      

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 5, 2008 6:14 PM

All replies

  • User-1136466523 posted

    Hi,

    From your description, it seems that you are unable to load the embedded resource ,right?

    Could you please make sure that the namespace is correct? Or if the resource matches DefaultNamespace.Filename.Extension. Besides, try to follow the article below:

    http://aspnet.4guysfromrolla.com/articles/080906-1.aspx

    Thanks.

    Monday, March 3, 2008 12:20 AM
  • User-649410219 posted

    Hi Michael,

    The Namespace is exactly the same (with lower and upper case).

    Before I post this question, I already had seem some articles, including you mentioned above.

    I tried to change the Namespace with all possible combinations, but it wont work.

    My project (VB), was the Namespace, e.g. "MyNameSpace.MyProject". I already try to use (in AssemblyInfo.vb and MyCustomControl):

    • MyNameSpace.MyProject.Resources.test.gif
    • MyNameSpace.MyProject.test.gif
    • MyProject.Resources.test.gif
    • MyProject.test.gif
    • Resources.test.gif 
    • test.gif
    Monday, March 3, 2008 9:59 AM
  • User-16411453 posted

     I saw your post, and I had the same problem but figured it out.  Part namespace and part GetType().  The namespace is the exact name you typed in the project application tab, root namespace. properties box.

    I had to dump the web resource stuff and folder Resource, and place all the images in the project regular style, then tag them as embedded resources.

    Hope it helps, It's frustrating trying to figure it out.  All the different examples become confusing. 

    In the assembly info I placed

    <Assembly: System.Web.UI.WebResource("ICE5Software.teamRoster.PlayAudioButton.gif", "image/gif")>

    Protected Overrides Sub Render()
    
             Dim cs As ClientScriptManager = Me.Page.ClientScript
             Dim rsType As Type = Me.GetType()
    
        	ibSoundLink = New ImageButton
        	ibSoundLink.ImageUrl = cs.GetWebResourceUrl(rsType, "ICE5Software.teamRoster.PlayAudioButton.gif")
        	ibSoundLink.AlternateText = "Hover to play introduction"
        	ibSoundLink.Attributes.Add("onmouseover", "soundManager._writeDebug('<b>Demo 1a</b>'); soundManager.play('mySound0','/teamRoster/SoundFiles/CRASH_1.mp3');")
       	ibSoundLink.CausesValidation = False
    
        	ibSoundLink.OnClientClick = "soundManager._writeDebug('<b>Demo 1a</b>'); soundManager.play('mySound0','" & [SoundSource] & "');"
        	Controls.Add(ibSoundLink)
        	ibSoundLink.RenderControl(writer)
    
        End Sub 
    
    
     
    Tuesday, March 4, 2008 7:01 PM
  • User-649410219 posted

    Well, I think that this question is unsolved. To release any doubts, I do this, but the images didn't appear.

    I already create a new Project, with an resource image and one page. The problem is the same. 

    The resource image really works?

    Wednesday, March 5, 2008 8:51 AM
  • User-16411453 posted

    I could not get the resource image to work.  I had to place the images in the root folder, or the same place the web custom control is, an change the attribute again to embeded resource.  Then I had to delete the resources that were registered so that the images are just file taged embedded content.

     

    Post your code if you want to make it work.

    Wednesday, March 5, 2008 12:10 PM
  • User-649410219 posted

    I did some new tests and discover that my problem is VB. Unfortunately, company I work uses only VB.

    I just finish the "last" test with resources. I create 2 identical projects in VB and C#. Both have one image, marked as Embedded Resource, one AssemblyInfo with reference to the image embedded resource and one Class.

    • The VB Project has the Root Namespace "MyNamespace.ResourceTestVB"
    • The C# Project has the Default Namespace "MyNamespace.ResourceTestCS" 

    VB Class

    Imports System
    Imports System.Web.UI.WebControls

    Public Class MyCustomControl
    Inherits CompositeControl

    Protected Overrides Sub CreateChildControls()
    Dim img As New ImageButton
    img.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyNamespace.ResourceTestVB.Resources.Sunset.jpg")
    Me.Controls.Add(New System.Web.UI.LiteralControl("'CustomControlVB':<br />"))
    Me.Controls.Add(img)
    MyBase.CreateChildControls()
    End Sub

    End Class

    C# Class

    using System;
    using System.Web.UI.WebControls;

    namespace MyNamespace.ResourceTest
    {
    public class MyCustomControl:System.Web.UI.WebControls.CompositeControl
    {
    protected override void CreateChildControls()
    {
    ImageButton img = new ImageButton();
    img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyNamespace.ResourceTestCS.Resources.Sunset.jpg");
    this.Controls.Add(new System.Web.UI.LiteralControl("'CustomControlCS':<br />"));
    this.Controls.Add(img);

    base.CreateChildControls();
    }
    }
    }

    Is VB a problematic language to use image (or others content) embedded resource?

    Another question that I didn't found answer: What the difference between Default (C#) and Root (VB) Namespaces?

    Thanks for all!!!
    Danilo
     

    Wednesday, March 5, 2008 3:45 PM
  • User-16411453 posted

    working code

    working code

     

     

     

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls


    <DefaultProperty("Text"), ToolboxData("<{0}:Test runat=server></{0}:Test>")> _
    Public Class Test
    Inherits WebControl

    Protected Overrides Sub CreateChildControls()

    Dim cs As ClientScriptManager = Me.Page.ClientScript
    Dim rsType As Type = Me.GetType()

    Dim img As New ImageButton
    img.ImageUrl = cs.GetWebResourceUrl(rsType, "ICE5Software.teamRoster.PlayAudioButton.gif")
    Me.Controls.Add(img)

    MyBase.CreateChildControls()

    End Sub

    End Class
      

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 5, 2008 6:14 PM
  • User-649410219 posted

    Hum...

    According your examples and tests I had done, then I conclude that the images (or other resources) must be in project root. That's correct?

    All examples I tried, my images was in Resource folder in both VB and C# projects, but in C# it works. When I change (in VB) to root folder it works fine.

    Very strange! [^o)]

    I'll do more tests to try to find something else.

    Thank you very very much !!!!!! [:D] 

    Thursday, March 6, 2008 8:48 AM
  • User-16411453 posted

    I have no clue why it did not work in the Resource Folder.  Mine is modeled from a CS example I found from MSDN.

    But you have to get it to work first, then you can take it to the next level.

    If someone wishes to elaborate on that, please feel free to post your suggestion or knowledge of proper use of the resource folder.

    Make it a great day! 

    Thursday, March 6, 2008 1:54 PM
  • User-1924877455 posted
    jkirkerx wrote: > I have no clue why it did not work in the Resource Folder. Mine is > modeled from a CS example I found from MSDN. > > But you have to get it to work first, then you can take it to the > next level. > > If someone wishes to elaborate on that, please feel free to post your > suggestion or knowledge of proper use of the resource folder. > > Make it a great day! Are you using Namespace.Foldername.Resourcename. I guess the correct is: AssemblyName.FolderName.ResourceName and the resource should be embedded inside the assembly. -- Madhur http://madhurahuja.blogspot.com
    Sunday, March 9, 2008 3:14 AM
  • User1825402209 posted

    I had similar problem and I did not give the folder name in the path

    and my namespace and assembly name was same just to make my things easy


    and it worked with this in the assemblyinfo and same path in while setting the image

    <Assembly: System.Web.UI.WebResource("<assembly or namespace>.<image>gif", "image/gif")>

    Monday, September 21, 2009 12:25 PM
  • User183500092 posted

    Just my two cents :

    For me, it started to work correctly when I stopped using GetType() to get the type to send to GetWebResourceUrl.

    When you use GetType() your risk getting a reference to an assembly that is dynamically compiled by ASP.Net and that doesn't contain the resources.

    When you use typeof(TypeInTheAssembly) you get a reference to the assembly that you compiled yourself containing the embedded resources.

    Monday, November 16, 2009 9:48 AM
  • User-406926163 posted

    Check that Buid Action is set to Embedded Resource for the image in Solution Explorer > Properties.

    Also, use Project Properties > Resource to add resources, not just add a file to the project.

    Regards,

    Jack

    Thursday, March 11, 2010 5:34 AM
  • User-1575438868 posted

    Step 1) to 3) you got right. Then, the confusion happens, because there are differences in how this is handle in VB vs C#.

    The important thing in VB is that the image path is not part of the resource name as it is in C#, so your example would be:

     

    4) In AssemblyInfo.vb:

    <Assembly: WebResource("<My Namespace>.test.gif", "image/gif")>
    


    5) In CreateChildControl : 

    '(...)
    Dim img as new Image
    img.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(Me.GetType(), "<My Namespace>.test.gif")
    '(...)
    Me.controls.add(img)


     

     

    Friday, March 12, 2010 3:35 AM
  • User-1545557919 posted

    I am using C# and the only way I could get this to work was specifying the resource as <NameSpace>.<Folder>.<FileName>.<FileExtension>. For the type parameter, I used the this.GetType()

    Note: My structure of the code is as follows:

    1. I have a custom control in a separate assembly than the webpage consuming  that custom control.

    2. My resource is in the same assembly as the custom control and it is the custom control that has the call to the GetWebResourceURL.

    Hope this information will be helpful to others.

    -Moiz Tankiwala PMP

    Tuesday, May 18, 2010 3:20 PM
  • User2090272125 posted

    This only works if you add something like .... (in my case i did this with a javascript)

    [assembly: WebResource("project.namespace.script.js", "text/javascript")]

    ... this line needs to go in the Assembly properties (Properties folder in the project root / AssemblyInfo.cs)

     

    Friday, November 4, 2011 8:44 AM