代码之家  ›  专栏  ›  技术社区  ›  Gavin Miller

使用嵌套项构建复合控件

  •  1
  • Gavin Miller  · 技术社区  · 16 年前

    <cc1:RadioButtonField ID="rfCardType" runat="server" Title="Card Type:">
        <asp:ListItem Enabled="true" Text="MasterCard" />
        <asp:ListItem Enabled="true" Text="Visa" />
        <asp:ListItem Enabled="true" Text="Amex" />
    </cc1:RadioButtonField>
    

    我想做的是通过 <asp:ListItems

    控制标记

    <div class="Title"> 
        <asp:Label ID="lblTitle" runat="server" AssociatedControlID="rblField" />
    </div>
    <div class="Text">
        <asp:RadioButtonList ID="rblField" runat="server" Visible="true">
        </asp:RadioButtonList>
    </div>
    

    对于单选按钮字段:

    ???
    

    背后的RadioButtonField代码需要做什么才能收集 <asp:列表项目 >并将它们传递给单选按钮列表?

    2 回复  |  直到 16 年前
        1
  •  2
  •   Dave Swersky    16 年前

    <ListItem> 样式标记,以下是您必须执行的操作:

    1. 添加一个 items 类型的私有字段 ListItemCollection 到您的复合控制
    2. 添加一个 Items 类型公共财产 列表集合 您的复合控制。getter应该引用 物品 私人领域。
    3. 添加 ParseChildren 将类属性添加到复合控件中,以便它读取列表。

    您的复合控件现在可以读取 ListItem 从其标记中提取节点。当控件呈现时,所有 <列表项> 节点将被添加到私有 物品

    如果你现在能设置一下,那就太好了 物品 RadioButtonList的成员,但不幸的是它是私有的。你必须 foreach 通过 物品 字段并调用 Add() 您的孩子RadioButtonList上的方法。

        2
  •  1
  •   Anne Jerry Qassar    14 年前
    [PersistenceMode(PersistenceMode.innerProperty)]
    [DesignerSerializationVisibility(DesignerSerilizationVisibility.Content)]
    ListCollection Items
    {
    }
    

    不妨试试这两个属性。 这 PersistChildrenAttribute 仅在Visual Studio中为您的控件提供设计器支持。 DesignerSerializationVisibility.Content 序列化集合数据类型的内容

    推荐文章