Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

x.ImageUrl = image.ImageUrl not Image x = image

I wonder if anyone can help me understand this conceptually.

I have an Image called dataImage and a form element of type Image called formImage.  In some of my code that gets called on certain PostBacks I say:

formImage = dataImage;

But dataImage does not appear on the page.

If I say:

formImage.ImageUrl = dataImage.ImageUrl

it appears fine as I expect.

Can anyone help me understand why this is so?

Thanks,

Tom

Thomas David Baker
Thursday, November 06, 2003

Because the Image Web Server Control doesn't have a default property.

qwerty
Thursday, November 06, 2003

Right, OK.  But if I have this:

Donkey x = new Donkey("Dobbin");
Donkey d = new Donkey("Muffin");
d = x;


Then d.Name is surely "Dobbin" and not "Muffin".  I've assigned a completely different object reference to the variable d, right?  I haven't tried to access d's default property.  Or have I?  Perhaps I'm confused.

In the same way I would expect

formImage = dataImage

to change the object reference held in formImage (if any) to an object reference that points to the already existing object pointed to by dataImage.

No?

Thomas David Baker
Thursday, November 06, 2003

You are confused. Web pages don't directly "embed" images, they only reference then via a URL. Therefore the asp:Image control only has a string property for setting the image URL, they don't support the assignment of an image blob/object to them.

Duncan Smart
Thursday, November 06, 2003

Ah! Right, yes, of course.  I was thinking of Image as representing an image file but really it represents the <img> tag more than anything.

Even so, if I replace one <img> tag with another then surely the URL in the new <img> should be used not the old one?  Or am I misinterpreting how much can be done in a postback.

I could do:

formImage.ImageUrl = dataImage.ImageUrl;
formImage.AltText = dataImage.AltText;
formImage.ToolTip = dataImage.ToolTip;

but I'd much rather do:

formImage = dataImage;

if that is in any way possible?

Thomas David Baker
Thursday, November 06, 2003

The first/easiest thing that comes to mind is overloading the Image control, and overloading the assignment operator, and/or assigning a default property to get the functionality you want.

Really though, seems like less work to just use image1.imageurl = image2.imageurl;

Greg Hurlman (blogs.squaretwo.net)
Friday, November 07, 2003

When you do formImage = dataImage, formImage ends up referencing the same object as dataImage.  This is not the object that appears on the web page.  The object that appears on the web page is not determined by whatever formImage references.  It's determined by what goes in the Controls collection.  formImage is just a reference to this object.  ASP.NET automatically sets formImage for you for your convenience.  If you change that reference by assigning to it, you lose that convenience (and gain nothing). 

You could probably do something like this:
    int i = Controls.IndexOf(formImage);
    Controls.RemoveAt(i);
    Controls.AddAt(i, dataImage);

This would replace the image that ASP.NET created for you with the one you created (though I guess this just replaces your three lines of code with a different three lines of code).

The more common way of doing something like this is to put a PlaceHolder on the page and then add your controls to that.  In other words:
    formImagePlaceHolder.Controls.Add(dataImage);

To be honest, I don't understand why it would ever be necessary or useful to do what you're trying to do.  Why not just set the properties on formImage in the first place?

SomeBody
Friday, November 07, 2003

Ah!  Final understanding dawns.  That makes it crystal, thanks.  And thanks to Duncan too where I first saw chinks of light!

I just thought my CircuitType object might as well carry an Image around as a string for the image URL but I see now why that isn't a useful thing for it to do.  Property Image becomes property ImageUrl on CircuitType and all my assignments change from = to .ImageUrl =

Thanks guys.

Thomas David Baker
Wednesday, November 12, 2003

Oh - one last note - the reason I wanted to do it was so ToolTip and AlternateText and all that stuff would all be set at once rather than requiring multiple calls.  Its now going to be a method -- SetImage(Image, CircuitType)

Thomas David Baker
Wednesday, November 12, 2003

*  Recent Topics

*  Fog Creek Home