Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在页面中找到所有DropDownList实例?_C#_Asp.net - Fatal编程技术网

C# 如何在页面中找到所有DropDownList实例?

C# 如何在页面中找到所有DropDownList实例?,c#,asp.net,C#,Asp.net,以下选项不起作用: foreach (Control control in Controls) { if (control is DropDownList) { DropDownList list = control as DropDownList; ... } } PS:我的类扩展了System.Web.UI.Page,可以轻松地用于任何类型的控件。

以下选项不起作用:

        foreach (Control control in Controls) {
            if (control is DropDownList) {
                DropDownList list = control as DropDownList;
                ...
            }
        }

PS:我的类扩展了
System.Web.UI.Page

,可以轻松地用于任何类型的控件。它还具有深入到层次结构中以查找嵌套在其他控件中的控件的好处。

这些控件可以轻松地用于任何类型的控件。它还具有深入层次结构以查找嵌套在其他控件中的控件的好处。

您只需将控件替换为Form.controls即可

foreach (Control c in Form.Controls)
{
  if (c is DropDownList)
  {
    // do something
  }
}

您只需要将控件替换为Form.Controls

foreach (Control c in Form.Controls)
{
  if (c is DropDownList)
  {
    // do something
  }
}

问题是某些DropDownList控件可能嵌套在其他控件中

如果页面有一个面板,并且所有控件都在该面板中,则页面的控件数组将仅具有该面板,并且所有控件都将位于该面板的控件数组中


ajax81链接到的链接将正常工作。

问题在于某些DropDownList控件可能嵌套在其他控件中

如果页面有一个面板,并且所有控件都在该面板中,则页面的控件数组将仅具有该面板,并且所有控件都将位于该面板的控件数组中


ajax81链接到的链接将正常工作。

或者您可以使用此扩展:

public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
    {
        bool hit = startingPoint is T;
        if (hit)
        {
            yield return startingPoint as T;
        }
        foreach (var child in startingPoint.Controls.Cast<Control>())
        {
            foreach (var item in AllControls<T>(child))
            {
                yield return item;
            }
        }
    }
公共静态IEnumerable AllControls(此控件起始点),其中T:Control
{
布尔命中=起始点为T;
如果(命中)
{
收益率返回起始点为T;
}
foreach(startingPoint.Controls.Cast()中的var child)
{
foreach(所有控件中的var项(子项))
{
收益回报项目;
}
}
}
然后,您可以使用它搜索特定控件中的任何类型的System.Web.UI.Control。如果是DropDownList,您可以像这样使用它:

IEnumerable<DropDownList> allDropDowns = this.pnlContainer.AllControls<DropDownList>();
IEnumerable allDropDowns=this.pnlContainer.AllControls();
  • 这将找到ID为“pnlContainer”的面板控件中的所有下拉列表

或者您可以使用此扩展:

public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
    {
        bool hit = startingPoint is T;
        if (hit)
        {
            yield return startingPoint as T;
        }
        foreach (var child in startingPoint.Controls.Cast<Control>())
        {
            foreach (var item in AllControls<T>(child))
            {
                yield return item;
            }
        }
    }
公共静态IEnumerable AllControls(此控件起始点),其中T:Control
{
布尔命中=起始点为T;
如果(命中)
{
收益率返回起始点为T;
}
foreach(startingPoint.Controls.Cast()中的var child)
{
foreach(所有控件中的var项(子项))
{
收益回报项目;
}
}
}
然后,您可以使用它搜索特定控件中的任何类型的System.Web.UI.Control。如果是DropDownList,您可以像这样使用它:

IEnumerable<DropDownList> allDropDowns = this.pnlContainer.AllControls<DropDownList>();
IEnumerable allDropDowns=this.pnlContainer.AllControls();
  • 这将找到ID为“pnlContainer”的面板控件中的所有下拉列表

如果其他控件中嵌套了dropdownlists,则不会出现这种情况吗?同意。我只是想指出所提供代码的明显问题。如果其他控件中嵌套了dropdownlists,这不会崩溃吗?同意。我只是想指出所提供代码的明显问题,我在BasePage类中添加了2方法。有一些错误:你能显示更多的代码吗?看起来您可能已将这些函数粘贴到程序范围之外。Pastebin.com非常适合粘贴大量代码。我在我的BasePage类中添加了2方法。有一些错误:你能显示更多的代码吗?看起来您可能已将这些函数粘贴到程序范围之外。Pastebin.com非常适合粘贴大量代码。