以前做嵌套,要么是在多次访问数据库,要么是把数据取出来,然后分别使用DataView的RowFilter来做,今天发现了一种新的方法,使用DataSet的Relations可以很方便的做嵌套
方法主要是在dataset的表之间建立关联,然后在前台html视图里使用
DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("MyRelation") %>'
具体例子如下:
这里用的是两个repeater嵌套
绑定代码:
Business.Customer obj = new Zeda.CRM.Business.Customer();
DataSet ds = obj.up_CRM_ProductSearchList(this.KeyWord);
ds.Relations.Add("MyRelation",ds.Tables[0].Columns["CustomerId"],ds.Tables[1].Columns["CustomerId"]);
ParentRep.DataSource = ds.Tables[0].DefaultView;
//ParentRep.DataBind();
AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
Page.DataBind();
ds.Dispose();
obj.Dispose();
DataSet ds = obj.up_CRM_ProductSearchList(this.KeyWord);
ds.Relations.Add("MyRelation",ds.Tables[0].Columns["CustomerId"],ds.Tables[1].Columns["CustomerId"]);
ParentRep.DataSource = ds.Tables[0].DefaultView;
//ParentRep.DataBind();
AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
Page.DataBind();
ds.Dispose();
obj.Dispose();
html视图代码
<asp:repeater id="ParentRep" Runat="server">
<ItemTemplate>
<tr>
<td height=25 width=40%> <a href='CustomerDetail.aspx?CustomerId=<%# DataBinder.Eval(Container.DataItem,"CustomerId")%>'><%# DataBinder.Eval(Container.DataItem,"Name")%></a></td>
<td><asp:Repeater ID=sonRep Runat=server DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("MyRelation") %>'>
<ItemTemplate>
<a href='ProductDetail.aspx?ProductId=<%# DataBinder.Eval(Container.DataItem,"[ProductId]")%>'><%# DataBinder.Eval(Container.DataItem, "[\"ProductName\"]")%></a>
</ItemTemplate>
<SeparatorTemplate>
</SeparatorTemplate>
</asp:Repeater></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td height="1" background="../images/xian01.gif" colspan="2"></td>
</tr>
</SeparatorTemplate>
</asp:repeater>
<ItemTemplate>
<tr>
<td height=25 width=40%> <a href='CustomerDetail.aspx?CustomerId=<%# DataBinder.Eval(Container.DataItem,"CustomerId")%>'><%# DataBinder.Eval(Container.DataItem,"Name")%></a></td>
<td><asp:Repeater ID=sonRep Runat=server DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("MyRelation") %>'>
<ItemTemplate>
<a href='ProductDetail.aspx?ProductId=<%# DataBinder.Eval(Container.DataItem,"[ProductId]")%>'><%# DataBinder.Eval(Container.DataItem, "[\"ProductName\"]")%></a>
</ItemTemplate>
<SeparatorTemplate>
</SeparatorTemplate>
</asp:Repeater></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td height="1" background="../images/xian01.gif" colspan="2"></td>
</tr>
</SeparatorTemplate>
</asp:repeater>