Nested DataPagers problem
Sample code
<asp:Repeater>
<ItemTemplate>
<asp:ListView DataSource=<%# Container.DataItem.Items %> ... />
<asp:DataPager .... />
</ItemTemplate>
<开发者_运维知识库/asp:Repeater>
This does not work.
The repeater data source is not a datasource control
It is set like so
repeater.DataSource = datasource
repeater.DataBind()It is possible, and I've done it many times before.
You may have to wire up the events yourself, and you do have to use FindControl()
in the repeaters item databound event to grab the specific ListView in order to set the Data Source, and also to call DataBind on it.
You can use the data binding shortcut <%# ... %>
within the nested Repeater / DataList, but not to set the DataSource as you have done.
Paste the following into a blank new project. Compiles and runs.
(Big disclaimer - the html is just for demonstration and is pretty poor.)
Web Form Code.
<asp:ListView ID="dlOuter" runat="server"
onitemdatabound="dlOuter_ItemDataBound">
<LayoutTemplate>
<div id="personGroupList">
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="groupHeading"><%# Eval("Key") %></div>
<asp:ListView ID="dlInner" runat="server"
ItemPlaceholderID="innerItemPlaceHolder"
onitemdatabound="dlInner_ItemDataBound"
>
<LayoutTemplate>
<asp:PlaceHolder ID="innerItemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div class="person">
Name: <%# Eval("Name") %>
Age: <%# Eval("Age") %>
</div>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
Now in the code behind
protected void Page_Load(object sender, EventArgs e)
{
// takes a list of Person and group's by Person.City
// really this is just an outer grouping that's in use.
var query = from p in Person.GetPersons() select p;
dlOuter.DataSource = query.ToLookup(o => o.City);
dlOuter.DataBind();
}
// The outer List View is the groups.
// we bind the inner view to the list if Person in the group.
protected void dlOuter_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem di = (ListViewDataItem)e.Item;
ListView inner = (ListView)e.Item.FindControl("dlInner");
IGrouping<string, Person> lookup = (IGrouping<string, Person>)di.DataItem;
inner.DataSource = lookup.AsEnumerable();
inner.DataBind();
}
}
protected void dlInner_ItemDataBound(object sender, ListViewItemEventArgs e)
{
// included so you can see how it's wired up. Unused in this sample.
}
Person class is just for demonstration.
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
public static List<Person> GetPersons()
{
List<Person> persons = new List<Person>
{
new Person { Name="Bob", Age=30, City="Chicago" },
new Person { Name="Mary", Age=20, City="NYC" },
new Person { Name="Marty", Age=12, City="LA" },
new Person { Name="Fred", Age=33, City="NYC" },
new Person { Name="Susan", Age=22, City="Chicago" }
};
return persons;
}
}
Note that in this example I'm grouping using ToLookup to create a grouping from a list. In the real code this is derived from, it's showing a page of data ordered by what happens on a particular day. E.g. the records are sorted by thing.SomeDate
and the grouping is query.ToLookup( o => o.SomeDate.ToLongDateString() );
It's important to note that the ToLookup
and use of IGrouping<T,X>
is irrelevant except I need to get grouped data in there somehow for the purposes of example. You could just as easily have the canonical example of Order
and OrderDetail
where the outer ListView is a List<Order>
and the inner ListView is the Order.OrderDetails
精彩评论