Javascript and ImageButton control

Mondeo

Centurion
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have an imagebutton control, i'm setting its src property using javascript as its part of an image swap type routine.

But then when its .net click event fires and need to know the filename of the actual image - i.e. the src attribute. However Imagebutton.ImageURL is nothing. Is there anyway I can get at the underlying image src filename?

Thanks
 
Use a hidden form field

The src attribute of an image is not posted back to the server. If you want to post it back I suggest you add a hidden input field to your form, and modify your Javascript to change the value of this field when the image URL is changed. When the form is posted back to the server you can query this hidden form field to retrieve the image URL.

Good luck :cool:
 
Thanks for that, i'm getting an error message though. This is my script (the bold is the line i've added)

function ChangeSource(imageID)
{
document.getElementById('imgMain').src = document.getElementById(imageID).src
document.myform.picid.value = document.getElementById('imgMain').src;
}

The error is document.myform.picid is null or not an object

My form declaration is <form runat=server name="myform">

And the field is <input type="hidden" id="picid">

Did I miss something? Thanks
 
getElementById

Strange that you use the more widely supported getElementById but then on the next line you use the DOM-style which is less reliable in my experience. Try this:

Code:
var imgID = document.getElementById(imageID);

document.getElementById('imgMain').src = imgID.src;
document.getElementById('picid').value = imgID.src;

As marble_eater mentioned, make sure your hidden field has both a name and an id attribute.

Good luck :)
 
Back
Top