2

I am going to create a web server control representing a treeview. So I want to use 2 images for + and - for expand/collapse. How can I build this into the control in a way that can be used as image source when rendered on the page?

Since this will be in a compiled web controls library, I don't want to rely on external images in the web application.

Edit:
Based on this answer by Andre Kraemer I did the following:

In AssemblyInfo.vb:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.minus.gif", "image/gif")> 

In my RenderContents override:

Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)

My Assembly name is MyWebControls.

My Root Namespace is MyCompany.MyWebControls.

The images plus.gif and minus.gif are located in a folder named Resources, and the images have Build Action set to Embedded Resource.

It still does not work. I get no errors. I have tried the generated image url directly in the browser, bot nothing happens, just a blank page.

Note:
I tried to use an invalid path in the resource name, and the result was exactly the same, which made me wonder if I need to do something special to map the actual resource to the resource name. I got a 404 Not Found error only if I used different name in the code than what was specified in AssemblyInfo, it had nothing to do with path was pointing to an actual resource!

Edit:

I found the solution!

I found out that it is a difference between C# and VB. See my own answer to this question.

Community
  • 1
  • 1
awe
  • 21,938
  • 6
  • 78
  • 91

3 Answers3

4

add the two images to a subfolder called images of your treeview control project. Then change their build action in the property grid from content to embedded resource.

In addition to that, you have to register those two images as embedded resources for your assembly in the assemblyinfo.cs file like this:

[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.plus.gif", "img/gif")]
[assembly: System.Web.UI.WebResource("YourProjectsNameSpace.Images.minus.gif", "img/gif")]

As those images will be embedded into your controls assembly now, you can access them using the ClientScriptManager like this:

string plusImageUrl  = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.plus.gif");
string minusImageUrl  = Page.ClientScript.GetWebResourceUrl(this.GetType(), "YourProjectsNameSpace.Images.minus.gif");
Andre Kraemer
  • 2,633
  • 1
  • 17
  • 28
  • I tried this, but the images does not show in the browser. By the way, I'm using VB, not C#, but I didn't think that would be much different (except syntax). – awe Feb 15 '10 at 14:19
  • Do you get some kind of error message, or do the pictures just not show? Did you have a look if plusImageUrl & minusImageUrl are assigned by the code? If so, did you try to copy those urls to the adress bar of your browser and open them directly? – Andre Kraemer Feb 15 '10 at 15:14
  • Nothing is shown if I try this url directly in the browser (not even an error). – awe Feb 16 '10 at 05:24
  • That's weired. If you have firefox with firebug installed: could you have a look at the network tab in firebug to see what's going over the wire when requesting the picture url directly in the browser? – Andre Kraemer Feb 16 '10 at 10:54
  • Ok, I found the solution at http://weblogs.asp.net/istofix/archive/2008/10/21/embedded-resources-in-vb-net-and-c-projects.aspx. It seems there are differences between C# and VB after all... – awe Mar 11 '10 at 10:59
2

OK, I found the answer in this article. I give credit to Andre Kraemer as the accepted answer, because he pointed me in the right direction. He could not know that there is a slight difference in the name structure between C# and VB!

Here is the solution that works for VB :

The important difference is that in VB, the file path is not included in the name.

In my example, the root namespace in the project is MyCompany.MyWebControls and the image files is in a subfolder named Resources. In C#, the subfolder is part of the resource name structure, but not in VB.

In C#, this would be:

[assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")]

In VB, we must ignore the file path in the resource name path:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.plus.gif", "image/gif")> 

So when we know this, my full example will be:

In AssemblyInfo.vb:

<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.plus.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.minus.gif", "image/gif")> 

In my RenderContents override:

Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)

And voilá, it works!

awe
  • 21,938
  • 6
  • 78
  • 91
1

Your image -> Properties -> Build Action -> Embedded Resource. It will be compiled with your control or library.

sashaeve
  • 9,387
  • 10
  • 48
  • 61
  • But how do I reference it in the `Image.ImageUrl` ? It needs to be something that returns a valid image url... – awe Feb 15 '10 at 09:58
  • C# - Reading an Embedded Resource file http://gibbons.co.za/archive/2005/02/02/253.aspx – sashaeve Feb 15 '10 at 10:09