I have an ASP.NET WebForms application that uses master pages and a single UpdatePanel
. Inside this UpdatePanel
, I have many other panels, which contain tables, which in turn, contain dropdown lists in the table cells.
I'd like to find all dropdown lists inside the panels and tables and check their SelectedValue
for a validation method I'm working on. So far I have only been successful in finding the UpdatePanel
and none of its child controls. I think I need to dig deeper into the respective sub-controls, but how to achieve this in a single method for all elements on the child page?
Master Page -> Child Page -> ContentPlaceHolder -> UpdatePanel -> Panel -> Table -> DropDownLists.SelectedValue
Have been trying many suggestions on SO, as well as Google, so far no dice. Any ideas?
In a nutshell, I'm looking to do something like this, but I think since my controls are nested in such a crazy way, the solution will end up being more complicated:
foreach(DropDownList ddl in d.Controls)
{
if (ddl.SelectedValue == "0")
HandleError(ddl.ID + " must have a value.");
}
Thanks for the reply @DMBeck.
I think I just found my answer, Good Ol' StackOverflow
This code from that answer seems to get me where I need to be:
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
}
}
And then...
foreach (Control c in EnumerateControlsRecursive(Page))
{
if (c is DropDownList)
{
ControlList.Add(c);
}
}
foreach(DropDownList d in ControlList)
{
if(d.SelectedValue == "0")
{
HandleError(d.ID + " must have a value.");
}
}