我想問我是否可以在我的控件中循環,如文本框,下拉列表等等,它們在頁面的用戶控件中。這種情況是,假設我有一個名爲'Page.aspx'的頁面,並且在page(uc1, uc2 and uc3)
中有3個用戶控件,並且在每個用戶控件中包含一個asp面板(名爲PnlTab1)以包含所有這些控件。 我使用的代碼來達到我的控件:如何在多用戶控件中循環控制?
UserControl uc1, uc2, uc3;
uc1 = usercontrol1;
uc2 = usercontrol2;
uc3 = usercontrol3;
foreach (Control c in uc1.FindControl("PnlTab1").Controls)
{
if (c is TextBox)
((TextBox)c).Enabled = true;
}
foreach (Control c in uc2.FindControl("PnlTab1").Controls)
{
if (c is TextBox)
((TextBox)c).Enabled = true;
}
foreach (Control c in uc3.FindControl("PnlTab1").Controls)
{
if (c is TextBox)
((TextBox)c).Enabled = true;
}
現在,我不想每次都「foreach (Control c in uc3.FindControl("PnlTab1").Controls
」寫的。我可以遞歸地做到這一點嗎?
謝謝大家!
謝謝你的回答,Tim,Dave和TheGeekYouNeed。 – enginbilici
我無法將用戶控件聲明爲數組。有這麼多的錯誤發生時,我試試這個。不幸的是,我遠程連接到我的程序,並有連接問題。我明天再試一次。謝謝你的回答。 – enginbilici
對不起,我在那裏閃回了c。這個新的語法應該是正確的。 – Dave