2012-12-26 43 views
1

我正在使用Asp.Net。我有一個充滿按鈕的頁面,每個按鈕都有一個id示例:btn_1_1,btn_1_2等。從代碼端我有一個循環,我使用隨機數字生成按鈕的Ids,例如:「btn_1_1」,「btn_1_2 「等。我的問題是,我如何使用此字符串值來訪問具有相同ID的按鈕的屬性?如何在ASP.Net中使用字符串ID訪問按鈕?

感謝

回答

3

您可以使用FindControl方法接受控件的ID作爲一個字符串。

string id = "btn_1_1"; 
Button btn1 = FindControl(id) as Button; 
if (btn1 != null) 
{ 
// Manipulating button's properties 
} 

只要確保你在呼喚按鈕上的最近的父FindControl,因爲這種方法不執行在控制樹進行遞歸搜索。

+0

謝謝我習慣了以下代碼,因爲我使用的是母版頁。再次感謝 ContentPlaceHolder cph =(ContentPlaceHolder)this.Master.FindControl(「ContentPlaceHolder1」); Response.Write(((Button)cph.FindControl(「a」))。Text); – Vince