在中继器中格式化数据

发布于 2024-12-18 22:34:33 字数 4224 浏览 0 评论 0原文

存储过程的结果将如下所示。

    1   Group Admin     user1    
    1   Group Admin     user2 
    1   Group Admin     user3
    1   Group Admin     user4 
    1   Group Admin     user5    
    2   Group Second    user6    
    2   Group Second    user7 
    2   Group Second    user8    
    2   Group Second    user9    
    2   Group Second    user10 
    2   Group Second    user11

我想格式化数据,如图所示。

这个问题与这个有关

我认为 Repeater 是此处使用的最佳控件。你认为呢 ?如果是这样,我该如何格式化它? 带有行合并的 GridView 是个好主意吗?

在此处输入图像描述

阅读答案后

// Page_Load
DataSet ds = GetAllUsers();

ViewState["UserRoles"] = ds;

rptParent.DataSource = ds;
rptParent.DataBind();

其他事件

// ItemDataBound
 protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if ((item.ItemType == ListItemType.Item) ||
                (item.ItemType == ListItemType.AlternatingItem))
            {
                string s = e.Item.DataItem.ToString();
                string roleID = ((HiddenField)e.Item.FindControl("hidRoleId")).Value;
                Repeater childRepeater = (Repeater)item.FindControl("rptChild");
                string filterExp = "RoleId=" + roleID;
                DataTable dtChild = ((DataSet)ViewState["UserRoles"]).Tables[0];
                DataRow[] dr = dtChild.Select(filterExp);
                childRepeater.DataSource = dr;
                childRepeater.DataBind();
            }
        }

标记

// MarkUp
<asp:Repeater runat="server" ID="rptParent" OnItemDataBound="rptParent_ItemDataBound">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:HiddenField ID="hidRoleId" runat="server" Value='<%#Eval("RoleId")%>' />
                            <asp:Label ID="lblRoleName" runat="server" Text='<%#Eval("RoleName")%>'></asp:Label>
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox4" runat="server" />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox5" runat="server" />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox6" runat="server" />
                        </td>
                    </tr>
                </table>
                <asp:Repeater ID="rptChild" runat="server">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:HiddenField ID="hidRoleId" runat="server" Value='<%#Eval("UserId")%>' />
                                    <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "UserName") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox7" runat="server" />
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox8" runat="server" />
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox9" runat="server" />
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

我得到以下输出

在此处输入图像描述

但组名称也在重复:(。这不是我想要的

The result of a stored procedure will look like this.

    1   Group Admin     user1    
    1   Group Admin     user2 
    1   Group Admin     user3
    1   Group Admin     user4 
    1   Group Admin     user5    
    2   Group Second    user6    
    2   Group Second    user7 
    2   Group Second    user8    
    2   Group Second    user9    
    2   Group Second    user10 
    2   Group Second    user11

I want to format the data as shown in picture .

The question is related with this

I think Repeater is the best control to use here. What you think ? If so, how can I format it ?
Is GridView with row merging a good idea ?

enter image description here

After reading the answer

// Page_Load
DataSet ds = GetAllUsers();

ViewState["UserRoles"] = ds;

rptParent.DataSource = ds;
rptParent.DataBind();

Other events

// ItemDataBound
 protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if ((item.ItemType == ListItemType.Item) ||
                (item.ItemType == ListItemType.AlternatingItem))
            {
                string s = e.Item.DataItem.ToString();
                string roleID = ((HiddenField)e.Item.FindControl("hidRoleId")).Value;
                Repeater childRepeater = (Repeater)item.FindControl("rptChild");
                string filterExp = "RoleId=" + roleID;
                DataTable dtChild = ((DataSet)ViewState["UserRoles"]).Tables[0];
                DataRow[] dr = dtChild.Select(filterExp);
                childRepeater.DataSource = dr;
                childRepeater.DataBind();
            }
        }

Mark up

// MarkUp
<asp:Repeater runat="server" ID="rptParent" OnItemDataBound="rptParent_ItemDataBound">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:HiddenField ID="hidRoleId" runat="server" Value='<%#Eval("RoleId")%>' />
                            <asp:Label ID="lblRoleName" runat="server" Text='<%#Eval("RoleName")%>'></asp:Label>
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox4" runat="server" />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox5" runat="server" />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox6" runat="server" />
                        </td>
                    </tr>
                </table>
                <asp:Repeater ID="rptChild" runat="server">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:HiddenField ID="hidRoleId" runat="server" Value='<%#Eval("UserId")%>' />
                                    <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "UserName") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox7" runat="server" />
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox8" runat="server" />
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox9" runat="server" />
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

I get the following output

enter image description here

But the Group name is also repeating :(. This is NOT what I want

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-12-25 22:34:33

我会使用嵌套中继器。

使用 Linq,您可以轻松地对数据进行分组并将其绑定到外部转发器(并设置顶部 3 个复选框的值),然后外部转发器的 OnDataBound 可以将用户列表设置为嵌套转发器的数据源。

标记看起来像:

<asp:Repeater runat="server" ID="ParentRepeater" OnItemDataBound="ParentRepeater_ItemDataBound">
    <ItemTemplate>
            // Markup for the three top checkboxes
            <asp:Repeater runat="server" ID="ChildRepeater">
                <ItemTemplate>
                   // Inner layout for each user with checkboxes
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </ItemTemplate>
</asp:Repeater>

在后面的代码中:

protected void ParentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    if ((item.ItemType == ListItemType.Item) ||
        (item.ItemType == ListItemType.AlternatingItem))
    {
        var data = item.DataItem as <YourType>;

        Repeater childRepeater = (Repeater)item.FindControl("ChildRepeater");
        childRepeater .DataSource = data.<InnerResults>;
        childRepeater .DataBind();
    }
} 

如果您需要使用匿名类型进行分组,请查看 Tuple<,>班级。如果您想将 item.DataItem 转换回已知类型,这会有所帮助。

I would use a nested repeater.

With Linq you can easily group your data and bind this to the outer repeater (and set the values for the 3 top checkboxes) and then the OnDataBound of the outer repeater can set the User list as datasource for the nested repeater.

Markup would look like:

<asp:Repeater runat="server" ID="ParentRepeater" OnItemDataBound="ParentRepeater_ItemDataBound">
    <ItemTemplate>
            // Markup for the three top checkboxes
            <asp:Repeater runat="server" ID="ChildRepeater">
                <ItemTemplate>
                   // Inner layout for each user with checkboxes
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </ItemTemplate>
</asp:Repeater>

And in the code behind:

protected void ParentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    if ((item.ItemType == ListItemType.Item) ||
        (item.ItemType == ListItemType.AlternatingItem))
    {
        var data = item.DataItem as <YourType>;

        Repeater childRepeater = (Repeater)item.FindControl("ChildRepeater");
        childRepeater .DataSource = data.<InnerResults>;
        childRepeater .DataBind();
    }
} 

If you need to use an anonymous type for your grouping have a look at the Tuple<,> class. That can help if you want to cast the item.DataItem back to a known type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文