每个不同用户的组中继器

时间:2013-08-05 12:56:24

标签: c# asp.net repeater grouping

我有以下转发器项目:

<asp:Repeater ID="RptLeaveRequests" runat="server" 
        onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
    <table id="tableItem" runat="server">
        <tr>
                <td style="width: 100px;">
                    <asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
                </td>
                <td style="width: 100px;">
                    <asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
                </td>
                <td style="width: 200px;">
                    <asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
                </td>
                <td style="width: 200px; font-size:10px;">
                    <asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
                </td>
                <td style="50px">
                    <asp:RadioButtonList ID="rbtVerified" runat="server" >
                        <asp:ListItem Value="1">Accept</asp:ListItem>
                        <asp:ListItem Value="2">Reject</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    <asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
                </td>
            </tr>
    </table>
</ItemTemplate>
</asp:Repeater>

页面加载我绑定了Repeater

else if(Convert.ToInt32(Session["UserLevel"]) != 0 && Convert.ToInt32(Session["UserLevel"]) < 151)
{
       RptLeaveRequests.DataSource = newLeaveLogic().GetManagerUnverifiedLeaveRequests(Convert.ToInt32(Context.User.Identity.Name));
        RptLeaveRequests.DataBind();
        hasRequests = true;
}

现在,如何为GetManagerUnferifiedLeaveRequests返回的每个不同用户分组Repeater项,后者在下面的方法中返回DataTable:

protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
       if (hasRequests)
       {
             //grouping
       }
}

1 个答案:

答案 0 :(得分:1)

我看到两种可能的解决方案:

1)使用嵌套转发器:

<asp:Repeater ID="RptGroups" runat="server" onitemdatabound="RptGroups_ItemDataBound">
    <ItemTemplate>
        <asp:Repeater ID="RptLeaveRequests" runat="server"  onitemdatabound="RptLeaveRequests_ItemDataBound">
            <ItemTemplate>
                ...
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

2)对组使用单个转发器,并使用例如DataList或其他控件渲染每个组以获取项目列表。

在这种情况下,您可以在此处将组列表绑定到数据源:

 RptLeaveRequests.DataSource = itemsGroups;
 RptLeaveRequests.DataBind();

并在此处绑定组项目列表:

protected void RptLeaveRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
      //bind here list of group items
}

我更喜欢第二个版本。