tags:

views:

1259

answers:

4

Hi, I know I can get an imagebutton's X&Y, but how do I get it's ID?

I'd like to start by printing it to a label at first. later I would like to use it in a switch case - any different case would change the imagebutton.imageurl to a different image, but speicifically do it for the imagebutton I just clicked on.

I tried

Label1.Text = Convert.ToString((ImageButton)sender);

But this is the result

 System.Web.UI.WebControls.ImageButton

As a result which isn't a lot of help, because I need the specific control's ID.

Thanks!

+2  A: 
((ImageButton)sender).ClientID

or if you want just the ID

((ImageButton)sender).ID
Kevin
+5  A: 

Do you mean this?

  Label1.Text = ((ImageButton)sender).ID

Update (as per your comment):

To change the ImageURL, you would use this:

((ImageButton)sender).ImageUrl ="correct_quiz.gif";

Or if you'd want to combine the two things, I'd recommend this:

ImageButton button = ((ImageButton)sender);
Label1.Text = button.ID;
button.ImageUrl = "correct_quiz.gif";
M4N
I guess I did! :) Thanks!Is it possible to use this in order to change the current imagebuttons ImageURL, like so? ((ImageButton)sender).ID).ImageUrl ="correct_quiz.gif";
Sarit
+2  A: 
ImageButton b = sender as ImageButton;
if (b != null) {
    string theId = b.ClientID;

    // Change the URL
    b.ImageUrl = "/whatever/you/like.png";
}
Sean Bright
I tried that and it's working!Is it possible to use this in order to change the current imagebuttons ImageURL, like so? ((ImageButton)sender).ID).ImageUrl ="correct_quiz.gif";
Sarit
I've updated my answer to address your question.
Sean Bright
A: 

Try use ImageButton's ID property

sjthebat