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

时间:2015-02-10 21:32:29

标签: c# asp.net controls updatepanel master-pages

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

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

母版页 - >儿童页面 - > ContentPlaceHolder - > UpdatePanel - >面板 - >表 - > DropDownLists.SelectedValue

对SO以及Google一直在尝试很多建议,到目前为止还没有骰子。有什么想法吗?

简而言之,我正在寻找这样的事情,但我认为既然我的控件是以一种疯狂的方式嵌套的,那么解决方案最终会变得更加复杂:

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

2 个答案:

答案 0 :(得分:0)

听起来你应该做的是客户端验证与后面代码中的on-click事件中的服务器端验证(假设你使用的是旧的webform,而不是MVC)。

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

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

答案 1 :(得分:0)

感谢@DMBeck的回复。

我想我刚刚找到了答案,Good Ol'StackOverflow

这个答案的代码似乎让我得到了我需要的地方:

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.");
        }
    }
相关问题