1

我有一个这样构造的转发器“语句”:

<asp:Repeater ID="Statements" runat="server"><ItemTemplate><ul>
    <li>
        <div>
            <asp:Label Text='<%#("Problem Statement: " + Eval("Statement"))%>' runat="server"></asp:Label></div>
        <div>
            <asp:Label Text='<%#("Goal: " + Eval("Goal"))%>' runat="server"></asp:Label></div>
        <asp:CheckBoxList ID="Objectives" runat="server" RepeatDirection="Horizontal" RepeatColumns="2">
        </asp:CheckBoxList>
    </li>
</ul>
</ItemTemplate>
</asp:Repeater>

我试图从重复的 中提取所有已检查的项目CheckBoxList,但到目前为止,还没有运气。下面给了我一个无效的转换错误,但我的其他尝试也导致了失败。如何使用 LINQ 从重复列表中提取所有选中的项目?

Dim list = _
  (From c As Control In Me.FindControl("Statements").Controls _
  Where c.GetType() Is GetType(CheckBoxList) _
  Select c).ToList()

Dim selected = _
  From c As CheckBoxList In list.AsEnumerable() _
  Select c.Items.AsQueryable().Cast(Of ListItem).Select(Function(l) l.Selected = True)

提前致谢。

4

2 回答 2

3

您可以旋转语句转发器的 RepeaterItems 并使用 linq 抓取所有选定的复选框(仅适用于 AlternatingItem 和 Item 类型)。

        Dim lstAllSelected As New List(Of ListItem)

        Statements.Items.ForEach(Sub(rpi As RepeaterItem)
                                     If rpi.ItemType = ListItemType.AlternatingItem OrElse rpi.ItemType = ListItemType.Item Then
                                         Dim lstNewItems As List(Of ListItem) = (From li As ListItem In CType(rpi.FindControl("Objectives"), CheckboxList).Items _
                                                                                 Where li.Selected).ToList()
                                         If Not lstNewItems Is Nothing AndAlso lstNewItems.Count > 0 Then
                                             lstAllSelected.AddRange(lstNewItems)
                                         End If
                                     End If
                                 End Sub)
于 2013-06-06T15:13:35.660 回答
1

这将为您提供ListItem每个中继器项目中选定的 s 列表。

Dim selected = (From itm As RepeaterItem In Statements.Items
            From li As ListItem In DirectCast(itm.FindControl("Objectives"), CheckBoxList).Items
            Where li.Selected
            Select li).ToList
于 2013-06-06T15:47:30.960 回答