递归获取所有选中复选框的 ID

发布于 2024-11-05 18:31:09 字数 138 浏览 0 评论 0原文

我有一个名为 pnlCategories 的 ASP.NET 面板。我想要做的是创建一个函数,该函数返回在此面板中选中的所有复选框的列表通用列表。此函数必须遍历其他子控件(包括其他面板和表格)才能找到所有复选框。有人知道如何做到这一点吗?顺便说一句,这是 C#。

I have an ASP.NET panel called pnlCategories. What I am trying to do is create a function that returns a List generic list of all Check Boxes that are checked inside this panel. There are other child controls (including other panels and tables) that this function will have to traverse through to find all the check boxes. Anyone have any ideas how to do this? This is C# by the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-11-12 18:31:09

简单,也未经测试。这可以适用于仅收集控件 ID,但它的可重用性更高一些,并且非常适合放在公共库中。

    public static void FindControlsRecursive(Control root, Type type, ref List<Control> list)
    {
        if(root.Controls.Count != 0)
        {
            foreach(Control c in root.Controls)
            {
                if(c.GetType() == type)
                    list.Add(c);
                else if (c.HasControls())
                    FindControlsRecursive(c, type, ref list);
            }
        }
    }

及用法:

var checkboxes = new List<Control>();
FindControlRecursive(pnlCategories, typeof(CheckBox), ref checkboxes);

var ids = checkboxes.Select(c => c.UniqueID).ToList(); // or however you'd like to get them.

Simple, also untested. This could be adapted to only collect the controls IDs, but this is a little more reusable and is a great one to have in a common library.

    public static void FindControlsRecursive(Control root, Type type, ref List<Control> list)
    {
        if(root.Controls.Count != 0)
        {
            foreach(Control c in root.Controls)
            {
                if(c.GetType() == type)
                    list.Add(c);
                else if (c.HasControls())
                    FindControlsRecursive(c, type, ref list);
            }
        }
    }

And usage:

var checkboxes = new List<Control>();
FindControlRecursive(pnlCategories, typeof(CheckBox), ref checkboxes);

var ids = checkboxes.Select(c => c.UniqueID).ToList(); // or however you'd like to get them.
掩耳倾听 2024-11-12 18:31:09

我想说这样的东西(改编后)可能会起作用。我还没有测试过这个,但它应该能让你接近某个地方。

public List<CheckBox> FindAllCheckBoxControls(WebControl webControl)
{
     if(webControl.Controls.Count == 0)
         return new List<CheckBox>();

     var checkBoxes = webControl.Controls
          .Where(x => x.GetType() == typeof(CheckBox));
          .Select(x => x as CheckBox)
          .ToList();

     webControl.Controls.ToList().ForEach(control =>
        {
             checkBoxes.AddRange(FindAllCheckBoxControls(control));
        });

     return checkBoxes.Distinct();
}

I'd say something like this (adapted) might work. I haven't tested this, but it should get you somewhere close.

public List<CheckBox> FindAllCheckBoxControls(WebControl webControl)
{
     if(webControl.Controls.Count == 0)
         return new List<CheckBox>();

     var checkBoxes = webControl.Controls
          .Where(x => x.GetType() == typeof(CheckBox));
          .Select(x => x as CheckBox)
          .ToList();

     webControl.Controls.ToList().ForEach(control =>
        {
             checkBoxes.AddRange(FindAllCheckBoxControls(control));
        });

     return checkBoxes.Distinct();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文