如何绑定器使用的对象列表GROUPBY [英] How to bind Repeater with list of objects GroupBy
问题描述
首先,对不起,长细节。但其必要来形容我的问题。每一件事情是自explainatory没有那么难读。谢谢!
我从Object类的列表。 列表与LT; CalendarEvent> allCalendarEvents = GetEvents();
公共类CalendarEvent
{
公众诠释ID {搞定;组; }
公共标识的itemId {搞定;组; }
公开日期时间启动{搞定;组; }
公众的DateTime结束{搞定;组; }
公共字符串主题{搞定;组; }
}
现在我想组列表 allCalendarEvents
事件由启动
日期。所以我用这个code:
VAR monthList =从E在allCalendarEvents
E组由e.start.Month为G
排序依据g.Key
选择新{月= g.Key,事件= G};
中的所有列表中的项目现在是按月基。到现在为止还挺好。我用ASP中继到显示数据给用户。
< ASP:直放站=服务器ID =repMonthsOnItemDataBound =repMonths_OnItemDataBound>
<&ItemTemplate中GT;
<立GT;
<%#的eval(月)%GT;
code背后
repMonths.DataSource = monthList;
repMonths.DataBind();
我可以看到月数作为输出我的数据,例如在五月,六月,七月发生的事件,我看到:
5
6
7
现在我想展现给用户带来很多CalenderEvents是如何一组一个月内。如果我用用在 repMonths
另一个中继是这样的:
< ASP:直放站=服务器ID =repMonthsOnItemDataBound =repMonths_OnItemDataBound>
<&ItemTemplate中GT;
<立GT;
<%#的eval(月)%GT;
< ASP:直放站=服务器ID =repInnerEvent>
<&ItemTemplate中GT;
<立GT;
< ASP:标签=服务器ID =lblEventName>< / ASP:标签>
< /李>
< / ItemTemplate中>
< / ASP:直放站>
< /李>
< / ItemTemplate中>
< / ASP:直放站>
然后,我不知道如何与内部Repeater数据绑定。例如。
保护无效repMonths_OnItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
VAR项目= e.Item.DataItem为???; //我不知道会来这里
}
你需要做的就是找到真正的内部中继的第一件事。你是在正确的轨道上。使用的FindControl()
对直放站项目找到它。然后,一旦你拥有了它,只是简单地指定数据源和绑定。
保护无效repMonths_OnItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
ItemTemplate中> //我们只有在中继器产品的&lt的一部分照顾;
如果(e.Item.ItemType == ListItemType.Item)
{
直放站repInnerEvent =(中继器)e.Item.FindControl(repInnerEvent);
repInnerEvent.DataSource = //数据源
repInnerEvent.DataBind();
}
}
听起来就像是问题实际上不是如何绑定。它是如何获得的数据进行绑定。在这种情况下,你已经有你的查询编写。你只需要知道的月份。在这种情况下,我会用一个标签来保存一个月。
< ASP:直放站=服务器ID =repMonthsOnItemDataBound =repMonths_OnItemDataBound>
<&ItemTemplate中GT;
<立GT;
< ASP:标签ID =lblMonth=服务器文本='<%#的eval(月)%GT;'>< / ASP:标签>
< ASP:直放站=服务器ID =repInnerEvent>
<&ItemTemplate中GT;
<立GT;
< ASP:标签=服务器ID =lblEventName>< / ASP:标签>
< /李>
< / ItemTemplate中>
< / ASP:直放站>
< /李>
< / ItemTemplate中>
< / ASP:直放站>
然后在的ItemDataBound
,获得该标签和使用它的价值。
保护无效repMonths_OnItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
ItemTemplate中> //我们只有在中继器产品的&lt的一部分照顾;
如果(e.Item.ItemType == ListItemType.Item)
{
//获取一个月
标签lblMonth =(标签)e.Item.FindControl(lblMonth); //获取该月的事件
VAR EVENTLIST =从E在allCalendarEvents
其中,e.start.Month == lblMonth.Text
选择e; //绑定你的事件内中继
直放站repInnerEvent =(中继器)e.Item.FindControl(repInnerEvent);
repInnerEvent.DataSource = EVENTLIST
repInnerEvent.DataBind();
}
}
First of all, Sorry for long detail. but its necessary to describe my problem. Every thing is self explainatory not so hard to read. Thanks!
I have list of Object from my class. List<CalendarEvent> allCalendarEvents = GetEvents();
public class CalendarEvent
{
public int id { get; set; }
public ID itemId { get; set; }
public DateTime start { get; set; }
public DateTime end { get; set; }
public string subject { get; set; }
}
Now I wanted to group the events in the List allCalendarEvents
by start
date. So I used this code:
var monthList = from e in allCalendarEvents
group e by e.start.Month into g
orderby g.Key
select new { month = g.Key, Event = g };
All the items in the list are now group by Month. So far so good. I used ASP Repeater to show the data to the user.
<asp:Repeater runat="server" ID="repMonths" OnItemDataBound="repMonths_OnItemDataBound">
<ItemTemplate>
<li>
<%# Eval("month") %>
Code Behind
repMonths.DataSource = monthList;
repMonths.DataBind();
I can see the month number as output for my data, e.g. for the events in May, June,July, I see:
5
6
7
Now I want to show to the user how many CalenderEvents are inside one group of month. If I use use another repeater inside repMonths
like this:
<asp:Repeater runat="server" ID="repMonths" OnItemDataBound="repMonths_OnItemDataBound">
<ItemTemplate>
<li>
<%# Eval("month") %>
<asp:Repeater runat="server" ID="repInnerEvent">
<ItemTemplate>
<li>
<asp:Label runat="server" ID="lblEventName"></asp:Label>
</li>
</ItemTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
</asp:Repeater>
Then I don't know how to bind data with inner repeater. E.g.
protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
var item = e.Item.DataItem as ???; // I don't know what will come here
}
The first thing you'll need to do is actually find the inner repeater. You are on the right track. Use FindControl()
on the repeater item to find it. Then once you have it, just simply assign the data source and bind it.
protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
// We only care if the repeater item is part of the <ItemTemplate>
if(e.Item.ItemType == ListItemType.Item)
{
Repeater repInnerEvent = (Repeater)e.Item.FindControl("repInnerEvent");
repInnerEvent.DataSource = // your data source
repInnerEvent.DataBind();
}
}
Sounds like the question isn't actually how to bind. It is how to get the data to bind. In that case, you already have your query written. You just need to know the month. In that case, I would use a label to hold the month.
<asp:Repeater runat="server" ID="repMonths" OnItemDataBound="repMonths_OnItemDataBound">
<ItemTemplate>
<li>
<asp:Label ID="lblMonth" runat="server" Text='<%# Eval("month") %>'></asp:Label>
<asp:Repeater runat="server" ID="repInnerEvent">
<ItemTemplate>
<li>
<asp:Label runat="server" ID="lblEventName"></asp:Label>
</li>
</ItemTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
</asp:Repeater>
Then in the ItemDataBound
, get that label and use its value.
protected void repMonths_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
// We only care if the repeater item is part of the <ItemTemplate>
if(e.Item.ItemType == ListItemType.Item)
{
// Get the month
Label lblMonth = (Label)e.Item.FindControl("lblMonth");
// Get the events for the month
var eventList = from e in allCalendarEvents
where e.start.Month == lblMonth.Text
select e;
// Bind your events to the inner repeater
Repeater repInnerEvent = (Repeater)e.Item.FindControl("repInnerEvent");
repInnerEvent.DataSource = eventList
repInnerEvent.DataBind();
}
}
这篇关于如何绑定器使用的对象列表GROUPBY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!