I am trying to loop through every control in a panel and clear out all the contents if it is a DropDownList.
Here is what I have :
private void ClearOut()
{
foreach (Control list in MainPanel.Controls)
{
if (list.ToString().Equals("System.Web.UI.WebControls.DropDownList"))
{
//Clear it out here
}
}
}
This code does find every DropDownList, but then I cannot figure out how to clear them out once I get there. I can't use any properties of a DropDownList like selectedindex or items.clear().
using System.Linq;
using System.Web.UI.WebControls;
foreach (DropDownList list in MainPanel.Controls.OfType<DropDownList>())
{
list.Items.Clear();
}
or the same but manually:
foreach (Control c in MainPanel.Controls)
{
DropDownList list = c as DropDownList;
if (list != null)
{
list.Items.Clear();
}
}
Use this:
if(list is DropDownList)
{
DropDownList DDL = list as DropDownList;
DDL.Items.Clear();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.