2009-02-24 38 views
1

我知道我可以得到一个imagebutton的X & Y,但是如何得到它的ID?如何在C#中获得图像按钮ID?

我想首先将它打印到标签上。 后来我想在开关盒中使用它 - 任何不同的情况都会将imagebutton.imageurl更改为不同的图像,但特别是对于我刚刚单击的图像按钮而言。

我试图

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

不过这是结果

System.Web.UI.WebControls.ImageButton 

因此这不是一个很大的帮助,因为我需要的特定控件的ID。

谢谢!

回答

3
ImageButton b = sender as ImageButton; 
if (b != null) { 
    string theId = b.ClientID; 

    // Change the URL 
    b.ImageUrl = "/whatever/you/like.png"; 
} 
+0

我试过了,它的工作! 是否有可能使用它来改变当前的图像按钮ImageURL,像这样? ((ImageButton)sender).ID).ImageUrl =“correct_quiz.gif”; – Sarit 2009-02-25 06:50:47

2
((ImageButton)sender).ClientID 

,或者如果你只想要ID

((ImageButton)sender).ID 
6

你的意思呢?

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

更新(根据您的评论):

要改变IMAGEURL,你这样做:

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

或者,如果你想将两件事情结合起来,我推荐这个:

ImageButton button = ((ImageButton)sender); 
Label1.Text = button.ID; 
button.ImageUrl = "correct_quiz.gif"; 
+0

我想我做过了! :) 谢谢! 是否有可能使用它来改变当前的图像按钮ImageURL,像这样? ((ImageButton)sender).ID).ImageUrl =“correct_quiz.gif”; – Sarit 2009-02-25 06:19:33

0

尝试使用ImageButton的ID属性

相关问题