简体   繁体   English

无法枚举CodeBehind中的控件

[英]Can't enumerate controls in CodeBehind

I have defined html table not with the asp-tags one in my *.aspx page. 我在*.aspx页面中没有用asp-tags定义html表。 Then I have added some asp controls into this table in its rows as: <asp:CheckBox> . 然后,我在此表的行中添加了一些asp控件,如下所示: <asp:CheckBox>

I have defined a lot of checkboxes, because I need to provide for the user large amount of selections and I can't minimize the count of checkboxes. 我定义了很多复选框,因为我需要为用户提供大量选择,并且我不能减少复选框的数量。

In CodeBehind I wanted to enumerate the whole Controls to find, what I need by the GetType().Name and then added to temporary list for the future actions. CodeBehind我想通过GetType().Name枚举要查找的整个控件,然后将其添加到临时列表中以进行将来的操作。

But I've got a trouble, in the debugger I can't see any checkbox and the amount of the controls is very small. 但是我遇到了麻烦,在调试器中我看不到任何复选框,并且控件的数量很小。

I'm enumerating the controls in page like this: 我在页面中枚举如下控件:

foreach (var item in Page.Controls)
{
    if (item.GetType().Name == "CheckBox")
    {
         CheckBox checkBox = item as CheckBox;

         if (checkBox.ID != null && checkBox.ID != String.Empty && checkBox.Checked)
         {
             arrayDocs.Add(checkBox.ID);
         }
    }
}

The full code of my page: 我页面的完整代码:

aspx: http://pastebin.com/gUs2LchL aspx: http//pastebin.com/gUs2LchL

code behind: http://pastebin.com/5tfnKJSt 后面的代码: http : //pastebin.com/5tfnKJSt

As per MSDN the Control.Controls property returns a collection of the child controls of that control, and not descendants . 根据MSDN ,Control.Controls属性返回该控件的控件的集合,而不是后代的集合。 Only server side controls directly contained within that control will be part of the collection. 直接包含在该控件内的服务器端控件将成为集合的一部分。

If you want to build a collection of all the controls on the page you will need to recursively loop through the controls to add their child controls and so on - as Magnus correctly stated. 如果要在页面上构建所有控件的集合,则需要递归遍历这些控件以添加其子控件,依此类推-正如Magnus正确指出的那样。

The most efficient/performant way would be to build the collection from the parent container of the checkboxes, in this case the cells of HtmlTable "tableDocs". 最有效/最有效的方法是从复选框的父容器(在本例中为HtmlTable“ tableDocs”的单元格)构建集合。

Change- 更改-

foreach (var item in Page.Controls)

To- 至-

foreach (var item in tableDocs.Controls)

Then you will need to recursively loop through the child controls to at least two levels (row -> cells -> checkboxes) before you get to the checkboxes. 然后,在进入复选框之前,您需要递归地遍历子控件至至少两个级别(行->单元格->复选框)。

What may be far more straightforward is to use generics and create a typed List of CheckBox containing all the checkboxes you wish to test. 可能更直接的方法是使用泛型并创建一个包含您要测试的所有复选框的Checkbox类型列表。

Something like- 就像是-

var checkboxes = new List<CheckBox>{checkboxAuctionHistory, checkboxBackSaldo, checkboxBalanceDecrypted}; //And so on

    foreach (var checkbox in checkboxes)
    {
        if (!string.IsNullOrWhiteSpace(checkbox.ID) && checkbox.Checked)
            {
                arrayDocs.Add(checkbox.ID);
            }
        }
    } 

This has an obvious flaw in that you need to remember to alter the list every time you add a new checkbox to the form. 这有一个明显的缺陷,那就是每次向表单添加新复选框时都需要记住要更改列表。

Alternatively move the checkboxes out of a table or remove the runat="server" attribute from it, removing the table from the ASP.NET control tree, and making the container for the checkboxes "form1". 或者,将复选框从表中移出或从其中删除runat =“ server”属性,从ASP.NET控件树中删除该表,并将复选框的容器设置为“ form1”。

Most of the other markup on the page does not have runat="server" and therefore these elements are not part of the ASP.NET control tree. 页面上的其他大多数标记都没有runat =“ server”,因此这些元素不是ASP.NET控件树的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM