在中继器中格式化数据
存储过程的结果将如下所示。
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 ?
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
But the Group name is also repeating :(. This is NOT what I want
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用嵌套中继器。
使用 Linq,您可以轻松地对数据进行分组并将其绑定到外部转发器(并设置顶部 3 个复选框的值),然后外部转发器的 OnDataBound 可以将用户列表设置为嵌套转发器的数据源。
标记看起来像:
在后面的代码中:
如果您需要使用匿名类型进行分组,请查看 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:
And in the code behind:
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.