【问题标题】:Iterate through a ControlCollection from a CreateUserWizardStep从 CreateUserWizardStep 遍历 ControlCollection
【发布时间】:2010-10-25 10:37:19
【问题描述】:

我正在尝试遍历我的 CreateUserWizardStep 的 CreatedUser 事件中的 ControlCollection。我有一个 ContentTemplate ,其中包含一个充满复选框的表格,我使用这些复选框来收集一周内用户的可用性。为简洁起见,我将把我的代码粘贴到 pastebin 上。

Here 是指向 .aspx 页面的链接。 Here 是 CreatedUser 事件。

这个循环:

foreach (Control c in CreateUserWizardStep1.ContentTemplateContainer.Controls)
    {
        if (c.GetType() == typeof(CheckBox))
        {
        }
    }

给我一​​个 WizardDefaultInnerTable 而不是......好吧,它更容易使用。

我如何去获得第二个表中的复选框?我想要做的是找到选中的属性,然后构建可以放入数据库的字符串。任何指导表示赞赏。

谢谢!

【问题讨论】:

    标签: c# .net asp.net linq collections


    【解决方案1】:

    您必须递归地通过控件。但是,您也可以通过您设置的 id 来引用它们。

    递归解决方案如下所示:

    IEnumerable<T> FindControls<T>(Control parent) where T : Control {
       T t = parent as T;
       if (t != null) yield return t;
    
       foreach (Control c in parent.Controls) {
          foreach (var c2 in FindControls<T>(c)) yield return c2;
       }
    }
    

    【讨论】:

    • 马克,我无法让你的功能正常工作。然而,它引导我走上正确的道路!谷歌搜索递归迭代容器控件给了我this 页面。我稍微修改了一下以适应我的需要。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多