如何显示基于日期分类的通知

时间:2011-03-16 06:21:09

标签: facebook date notifications categories datalist

我想开发显示基于日期分类的数据的通知系统。喜欢facebook通知。请向我解释一下数据库设计。 我想在datalist / gridview(ASP.net / C#)

中显示这个

我告诉你这个例子:我想要的是什么。

  

今天   

<小时/>   阿德南预定了一个旅游(日期时间)
  Thor在主页上写了一个注释(DateTime)

     

昨天   

<小时/>   Roshan预订了一个巡演(DateTime)
  Raj在主页面上写了一个注释(DateTime)

     

3月16日   

<小时/>   Atif预订了一个旅游(日期时间)
  Naveed在主页面上写了一个注释(DateTime)

     

3月15日   

<小时/>   穆克什预订了一个旅游(日期时间)
  Rakesh在主页面上写了一个注释(DateTime)

     

3月14日   

<小时/>   阿德尔预订了一个旅游(日期时间)
  Farhan在主页面上写了一个注释(DateTime)

1 个答案:

答案 0 :(得分:2)

是的,我做到了。在Linq查询的帮助下。 :

解决方案:
1.我从数据库中获取了Notification数据到listOfNotifications
2.不同的日期。
3.创建另一个名为:Notification_Date的Business实体(您可以在代码中看到B.E)
4.在Notification_Date B.E撰写通知B.E.
5.循环遍历datesOnly并将值赋给Notification_Date
6.绑定到嵌套的datalist

List<Notifications> listofNotifications = new NotificationsDAL().GetAllNotifications();
                if (listofNotifications != null)
                {
                    dlNotifications.DataSource = GetNotificationWithDays(listofNotifications);
                    dlNotifications.DataBind();
                }



 public List<Notification_Date> GetNotificationWithDays(List<Notifications> listOfNotifications)
    {
        var datesOnly = (from i in listOfNotifications
                         select String.Format("{0:d/M/yyyy}", i.Datetime)).Distinct();

        List<Notification_Date> tempOfNotifications = new List<Notification_Date>();
        foreach (var onlyDate in datesOnly)
        {
            Notification_Date temp = new Notification_Date();
            temp.date = DateTime.ParseExact(onlyDate.ToString(), "d/M/yyyy", CultureInfo.InvariantCulture);
            temp.listOfNotifications = (from d in listOfNotifications       //Filter Data.
                                        where String.Format("{0:d/M/yyyy}", d.Datetime) == onlyDate
                                        select d).ToList();

            tempOfNotifications.Add(temp);
        }

        return tempOfNotifications;
    }


public class Notifications
{
    public long NID { get; set; }
    public int Type { get; set; }
    public DateTime Datetime { get; set; }
    public long Agent_id { get; set; }
    public string Agent_Name { get; set; }
    public Boolean is_read { get; set; }

}

public class Notification_Date
{
    public DateTime date { get; set; }
    public List<Notifications> listOfNotifications { get; set; }
}

外部数据库项目

protected void dlNotifications_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label lblDay = (Label)e.Item.FindControl("lblDay");
        string databaseDate = String.Format("{0:d/M/yyyy}", Convert.ToDateTime(lblDay.Text));
        string nowDate = String.Format("{0:d/M/yyyy}", DateTime.Now);

        TimeSpan timeSpan = DateTime.ParseExact(nowDate, "d/M/yyyy", CultureInfo.InvariantCulture) - 
            DateTime.ParseExact(databaseDate, "d/M/yyyy", CultureInfo.InvariantCulture); //Compare date.
        if (databaseDate == nowDate)
        {
            lblDay.Text = "Today";
        }
        else if (timeSpan.Days == 1)
        {
            lblDay.Text = "Yesterday";
        }
        else
        {
            lblDay.Text = String.Format("{0:MMMM, dd}", DateTime.ParseExact(databaseDate, "d/M/yyyy", CultureInfo.InvariantCulture));
        }
    }

NESTED DATALIST ITEMBOUND

protected void dldNotifications_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Notifications notification = (Notifications)e.Item.DataItem;
        Image imgAlert = (Image)e.Item.FindControl("imgAlert");
        Label lblInfo = (Label)e.Item.FindControl("lblInfo");
        LinkButton lnkLink = (LinkButton)e.Item.FindControl("lnkLink");

        if (!notification.is_read)
        { imgAlert.ImageUrl = "..\\images\\icon-s-msg.gif"; }
        else
        { imgAlert.ImageUrl = "..\\images\\icon-valid-fare.png"; }

        if (Convert.ToInt32(notification.Type) == Convert.ToInt32(Notification_Type.AgentBooking))
        {
            lblInfo.Text = "New agent <b>" + notification.Agent_Name + "</b> registerd and waiting for approval.   ";
            lnkLink.CommandName = Convert.ToInt32(Notification_Type.AgentBooking).ToString();
            lnkLink.CommandArgument = notification.NID.ToString();
        }
}

ASPx中的数据

<asp:DataList ID="dlNotifications" runat="server" 
        onitemdatabound="dlNotifications_ItemDataBound" Width="100%" 
        >
        <ItemTemplate>
        <div style="background-color:#F2F2F2; width:100%; height:24px; display: table; #position: relative; overflow: hidden;">
            <div  style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle; font-weight:bolder;">
            &nbsp;&nbsp; <asp:Label ID="lblDay" ForeColor="#333333" runat="server" Text='<%#Eval("date") %>'></asp:Label>
            </div>
            </div>
            <asp:DataList ID="dldNotifications" runat="server" 
                DataSource='<%# DataBinder.Eval(Container.DataItem, "listOfNotifications") %>' 
                Width="100%" onitemdatabound="dldNotifications_ItemDataBound" 
                onitemcommand="dldNotifications_ItemCommand">
                <ItemTemplate>
                <div style="width:100%; height:24px; display: table; #position: relative; overflow: hidden;">
            <div  style=" #position: absolute; #top: 50%;display: table-cell; vertical-align: middle; border-top-style:solid; border-top-color:#E9E9E9; border-top-width:thin;">
            &nbsp;&nbsp; 
                <asp:Image ID="imgAlert" runat="server" /> &nbsp; <asp:Label ID="lblInfo" runat="server" Text='<%# Eval("Type") %>'></asp:Label> 
                <asp:LinkButton ID="lnkLink" CommandArgument='<%# Eval("Type") %>' runat="server">link</asp:LinkButton>

                    <asp:Label ID="lblAgent_id" runat="server" Text='<%# Eval("Agent_id") %>' 
                    Visible="False"></asp:Label>

                    </div></div>
                </ItemTemplate>
            </asp:DataList>
            <br />
        </ItemTemplate>
    </asp:DataList>

这是输出: OutPut