如何獲取最後單擊面板上的對象的名稱?訣竅是面板上有一大排按鈕(btn [1] ... btn [200])。如何檢查我是否點擊了按鈕b [180]或b [11],甚至是否在面板外(無按鈕)?此外,這些按鈕通過編碼在頁面加載時生成。 謝謝。 Anna編號: 謝謝!出現的另一個問題(這產生了一個NULL對象引用): 我有一個方法在與buttonHandler()相同的級別上,它被命名爲HowManyClicked(),它在buttonHandler()中調用。內部HowManyClicked()我想確定Button btn1 = Panel2.FindControl(x)作爲Button;其中x例如是buttonArray [2,3]。但我總是得到NULL。 按鈕數組buttonArray是否在產生它的方法中無法通過名稱識別?單擊按鈕陣列上的事件
public void buttonHandler(object sender, EventArgs e)
{
Button btn = sender as Button;
//string tt = btn.ToolTip.ToString();
btn.BackColor = Color.Red;
statusL.Text = HowManyClicked().ToString();
}
public int HowManyClicked()
{
int sum=0;
for (int a = 0; a < 10; a++)
for (int b = 0; b < 14; b++)
{
string x = "buttonArray[" + a + ", " + b + "]";
statusL.Text = x;
Button btn1 = Panel2.FindControl(x) as Button;
if (btn1.BackColor == Color.Red) sum += 1;
}
return sum;
}
非常感謝Jupanol。另一件事:我有一個與buttonHandler()相同的方法,它被命名爲HowManyClicked(),它在buttonHandler()中被調用。內部HowManyClicked()我想確定Button btn1 = Panel2.FindControl(x)作爲Button;其中x是例如buttonArray [2,3]。但我總是得到NULL。按鈕數組buttonArray在生成它的方法中沒有按名稱標識:PopulateControls? –
爲什麼你不只是作爲參數傳遞給'HowManyClicked'按鈕'buttonHandler'就像這個'HowManyClicked(Button button)'和'buttonHandler {HowManyClicked(sender as Button)}' – Jupaol
那麼,HowManyClicked()是指在每次點擊後計算/更新,總共/總共點擊了多少個按鈕。所以這就是爲什麼我沒有使用方法簽名的參數。這更多的是一般性查詢,不涉及按鈕。 –