透過Page.Controls以foreach方法抓取網頁裡的控制項
透過Panel.Controls以foreach方法抓取內容控制項裏的控制項
這篇文章由[ASP.NET]Page.FindControl修正
透過Page.Controls以foreach方法抓取網頁裡的控制項
透過Panel.Controls以foreach方法抓取內容控制項裏的控制項
謝謝喵大與Allen Kuo指導修正
請問是否還有修正的空間呢?
感恩...
void ClearPanel()
{
foreach (object ctrl in Page.Controls)
{
//if (ctrl is System.Web.UI.WebControls.Panel)
if (ctrl is Panel)
{
//System.Web.UI.WebControls.Panel pl = (System.Web.UI.WebControls.Panel)ctrl;
Panel pl = (Panel)ctrl;
if (pl.ID.Contains("Panel") && pl.Visible == true)
{
foreach (object obj in pl.Controls)
{
//if (obj is System.Web.UI.WebControls.TextBox)
if (obj is TextBox)
{
TextBox tb = (TextBox)obj;
tb.Text = "";
}
else if (obj is Label)
{
Label lbl = (Label)obj;
lbl.Text = "";
}
else
{
DropDownList ddl = (DropDownList)obj;
ddl.SelectedIndex = 0;
}
}
pl.Visible = false;
}
}
}
{
foreach (object ctrl in Page.Controls)
{
//if (ctrl is System.Web.UI.WebControls.Panel)
if (ctrl is Panel)
{
//System.Web.UI.WebControls.Panel pl = (System.Web.UI.WebControls.Panel)ctrl;
Panel pl = (Panel)ctrl;
if (pl.ID.Contains("Panel") && pl.Visible == true)
{
foreach (object obj in pl.Controls)
{
//if (obj is System.Web.UI.WebControls.TextBox)
if (obj is TextBox)
{
TextBox tb = (TextBox)obj;
tb.Text = "";
}
else if (obj is Label)
{
Label lbl = (Label)obj;
lbl.Text = "";
}
else
{
DropDownList ddl = (DropDownList)obj;
ddl.SelectedIndex = 0;
}
}
pl.Visible = false;
}
}
}