按类型获取控件的所有子控件的列表

时间:2011-09-26 12:40:23

标签: asp.net detailsview

我试图创建一个方法,创建一个List,其名称为DetalsView的所有子控件的类型为string。

我最接近的是:

            foreach (Control c in dv.Controls)
            {
                if (c is Label)
                {
                    controlsToCheck.Add(c.ID);
                }
            }

            foreach (string s in controlsToCheck)
            {
                Label lbl = (Label)dv.FindControl(s);
                if (lbl.Text == "")
                {
                    lbl.Text = "None";
                    lbl.CssClass = "bold";
                }
            }

然而,所有这一切都是在第一个foreach中迭代一次,然后退出(即dv.Controls只返回一个项目)。如果我使用FindControl,我可以访问这些项目,但这意味着我必须为每个项目执行此操作。

有什么难事吗?

谢谢!

编辑:这是我的DetailsView(我剪掉了一些东西,其中只有更多控件,因此它适合页面):

        <asp:DetailsView DefaultMode="ReadOnly" FieldHeaderStyle-CssClass="dwHeader" CssClass="marginLeftRightBottom10px"
            AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" GridLines="None"
            ID="dvIndividualItem" runat="server" AutoGenerateRows="False" DataSourceID="sqldsSingleItem"
            OnDataBound="dvIndividualItem_DataBound">
            <Fields>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Name:</h1>
                        <p>
                            The name of the item.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblItemName" Text='<%# Bind("itemName") %>'></asp:Label>
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox runat="server" ID="tbItemName"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Description:</h1>
                        <p>
                            The description of the item.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblItemDescription" Text='<%# Bind("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox runat="server" ID="tbItemDescription"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Image:</h1>
                        <p>
                            The image of the item.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Image runat="server" ID="imgItem" Width="40px" Height="40px" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ReadOnly="true" runat="server" ID="tbItemImage"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Type:</h1>
                        <p>
                            Specifies the item type.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemType" Text='<%# Eval("itemType") %>' runat="server" />
                    </ItemTemplate>
                    <%--                            <InsertItemTemplate>
                        <asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="ddlItemTypes_SelectedIndexChanged"
                            DataTextField="itemType" DataValueField="typeId" DataSourceID="sqldsTier1Category"
                            ID="ddlItemTypes" runat="server">
                        </asp:DropDownList>
                        <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:myDbConnection%>" ID="sqldsTier1Category"
                            runat="server" SelectCommand="dbo.getItemCategories" SelectCommandType="StoredProcedure">
                        </asp:SqlDataSource>
                    </InsertItemTemplate>--%>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item SubType:</h1>
                        <p>
                            Specifies the sub-item type.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSubItemType" Text='<%# Eval("itemSubType") %>' runat="server" />
                    </ItemTemplate>
                    <%--                            <InsertItemTemplate>
                        <asp:DropDownList OnDataBound="ddlItemSubTypes_OnDataBound" AutoPostBack="true" DataTextField="itemSubType"
                            DataValueField="subTypeId" DataSourceID="sqldsTier2Category" ID="ddlItemSubTypes"
                            runat="server">
                        </asp:DropDownList>
                        <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:myDbConnection%>" ID="sqldsTier2Category"
                            runat="server" SelectCommand="dbo.getItemSubCategories" SelectCommandType="StoredProcedure">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="dwNewItem$ddlItemTypes" Name="typeId" PropertyName="SelectedValue"
                                    DbType="Int16" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </InsertItemTemplate>--%>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item SubSubType:</h1>
                        <p>
                            Specifies the sub-sub-item type.</p>
                        <p>
                            <i>Not always applicable.</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSubSubItemType" Text='<%# Eval("itemSubSubType") %>' runat="server" />
                    </ItemTemplate>
                    <%--                            <InsertItemTemplate>
                        <asp:DropDownList DataTextField="itemSubSubType" DataValueField="subSubTypeId" DataSourceID="sqldsTier3Category"
                            ID="ddlItemSubSubTypes" runat="server">
                        </asp:DropDownList>
                        <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:myDbConnection%>" ID="sqldsTier3Category"
                            runat="server" SelectCommand="dbo.getItemSubSubCategories" SelectCommandType="StoredProcedure">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="dwNewItem$ddlItemSubTypes" Name="subTypeId" PropertyName="SelectedValue"
                                    DbType="Int16" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </InsertItemTemplate>--%>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Requires Sword Swinger Class?</h1>
                        <p>
                            Specifies whether the item can only be used by the Sword Swinger.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbReqSwordSwinger" Text='<%# Bind("requiresSwordSwinger") %>' runat="server" />
                    </ItemTemplate>
                    <%--                            <InsertItemTemplate>
                        <asp:CheckBox runat="server" ID="cbReqSwordSwinder" />
                    </InsertItemTemplate>--%>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Requires Rat Catcher Class?</h1>
                        <p>
                            Specifies whether the item can only be used by the Rat Catcher.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbReqRatCatcher" Text='<%# Bind("requiresRatCatcher") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:CheckBox runat="server" ID="cbReqRatCatcher" />
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Requires Spell Mumbler Class?</h1>
                        <p>
                            Specifies whether the item can only be used by the Spell Mumbler.</p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbReqSpellMumbler" Text='<%# Bind("requiresSpellMumbler") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:CheckBox runat="server" ID="cbReqSpellMumbler" />
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-CssClass="colorBlue dwHeader">
                    <HeaderTemplate>
                        <h1>
                            Strength permanently added:</h1>
                        <p>
                            Specifies the amount of strength the item permanently adds to your character.</p>
                        <p>
                            <i>Only available when the item is set to type "Consumable"</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPermanentStrength" Text='<%# Bind("permanentStrength") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox Enabled="false" runat="server" ID="tbItemPermanentStr"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-CssClass="colorBlue dwHeader">
                    <HeaderTemplate>
                        <h1>
                            Agility permanently added:</h1>
                        <p>
                            Specifies the amount of agility the item permanently adds to your character.</p>
                        <p>
                            <i>Only available when the item is set to type "Consumable"</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPermanentAgility" Text='<%# Bind("permanentAgility") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox Enabled="false" runat="server" ID="tbItemPermanentAgl"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-CssClass="colorBlue dwHeader">
                    <HeaderTemplate>
                        <h1>
                            Magical Power permanently added:</h1>
                        <p>
                            Specifies the amount of magical power the item permanently adds to your character.</p>
                        <p>
                            <i>Only available when the item is set to type "Consumable"</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPermanentMagicalPower" Text='<%# Bind("permanentMagicalPower") %>'
                            runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox Enabled="false" runat="server" ID="tbItemPermanentMP"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-CssClass="colorBlue dwHeader">
                    <HeaderTemplate>
                        <h1>
                            Health Points restored:</h1>
                        <p>
                            Specifies the amount of health points the item restores.</p>
                        <p>
                            <i>Only available when the item is set to type "Consumable"</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblHPRestored" Text='<%# Bind("restoresHealthPoints") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox Enabled="false" runat="server" ID="tbItemRestoresHp"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-CssClass="colorBlue dwHeader">
                    <HeaderTemplate>
                        <h1>
                            Mana Points restored:</h1>
                        <p>
                            Specifies the amount of mana points the item restores.</p>
                        <p>
                            <i>Only available when the item is set to type "Consumable"</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblManaRestored" Text='<%# Bind("restoresMana") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox Enabled="false" runat="server" ID="tbItemRestoresMana"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-CssClass="colorBlue dwHeader">
                    <HeaderTemplate>
                        <h1>
                            Health Points permanently added:</h1>
                        <p>
                            Specifies the amount of health points the item permanently adds to your character.</p>
                        <p>
                            <i>Only available when the item is set to type "Consumable"</i></p>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPermanentHP" Text='<%# Bind("permanentHealth") %>' runat="server" />
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox Enabled="false" runat="server" ID="tbItemPermanentHP"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
            </Fields>
        </asp:DetailsView>

4 个答案:

答案 0 :(得分:7)

试试这个

    public static IEnumerable<Control> GetAllControls(Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            yield return control;
            foreach (Control descendant in GetAllControls(control))
            {
                yield return descendant;
            }
        }
    }

并致电

List<Control> ControlsToCheck = GetAllControls(dv).OfType<Label>().ToList();

答案 1 :(得分:1)

当您遍历dv.Controls时,它只显示DetalsView下第一级的控件。如果需要查找所有标签,则需要遍历所有子控件,然后是子项等。

@Bala R.的答案就是一个很好的例子。您还可以在this answer上找到一些示例。

答案 2 :(得分:1)

我稍微修改了Bala R的解决方案。我把它作为一种通用的扩展方法,只有你感兴趣的控制类型,所以你不需要再调用.OfType<T>

public static IEnumerable<T> GetControls<T>(this Control parent) where T : Control
{
    foreach (Control control in parent.Controls)
    {
        if (control is T) yield return control as T;
        foreach (Control descendant in GetControls<T>(control))
        {
            if (control is T)
                yield return descendant as T;
        }
    }
}

像这样使用:

List<Label> labels = dv.GetControls<Label>().ToList();

foreach(Label label in dv.GetControls<Label>())
{
    //do stuff
}

答案 3 :(得分:0)

这个链接帮助了我,我希望能够装备4 u2;)

http://msdn.microsoft.com/en-us/library/yt340bh4%28v=vs.100%29.aspx