如何在母版页的子页上查找 UpdatePanel 内的所有下拉列表

How to Find all Dropdown Lists inside of UpdatePanel on Child Page of a Master Page

我有一个 ASP.NET WebForms 应用程序,它使用母版页和一个 UpdatePanel。在这个 UpdatePanel 中,我还有许多其他面板,其中包含 table,而这些面板又包含 table 单元格中的下拉列表。

我想在面板和 table 中找到所有下拉列表,并检查它们的 SelectedValue 以了解我正在使用的验证方法。到目前为止,我只成功地找到了它的子控件的 UpdatePanel 和 none。我想我需要更深入地研究各个子控件,但是如何在子页面上的所有元素中用一个方法实现这一点?

母版页 -> 子页 -> ContentPlaceHolder -> UpdatePanel -> 面板 -> Table -> DropDownLists.SelectedValue

一直在尝试关于 SO 的许多建议,以及 Google,到目前为止还没有成功。有什么想法吗?

简而言之,我想做这样的事情,但我认为由于我的控件以如此疯狂的方式嵌套,解决方案最终会变得更加复杂:

foreach(DropDownList ddl in d.Controls)
{
    if (ddl.SelectedValue == "0")
        HandleError(ddl.ID + " must have a value.");
}

听起来你应该做的是在代码隐藏的点击事件中将客户端验证与服务器端验证结合起来(假设你使用的是旧的 webform,而不是 MVC)。

对于客户端验证,我建议使用 JQuery 验证 http://jqueryvalidation.org/

在服务器端,假设按钮位于更新面板内的子页面上,只需按 id 调用项目。

感谢@DMBeck 的回复。

我想我刚刚找到了答案,好样的 Ol' Whosebug

这个答案中的这段代码似乎让我到达了我需要去的地方:

IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control descendant in EnumerateControlsRecursive(child))
            yield return descendant;
    }
}

然后...

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.");
        }
    }