2009-02-24 53 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屬性

相關問題