开发者

how to show notifications categorized based on date

I want to develop notificatio开发者_JAVA技巧n system that show data categorized based on date. like facebook notifications. please kindly explain me about database design. and i want to show this in datalist/gridview (ASP.net / C#)

i show you the example: what i want.

Today


Adnan booked a tour (DateTime)

Thor write a note on the main page (DateTime)

Yesterday


Roshan booked a tour (DateTime)

Raj write a note on the main page (DateTime)

March 16


Atif booked a tour (DateTime)

Naveed write a note on the main page (DateTime)

March 15


Mukesh booked a tour (DateTime)

Rakesh write a note on the main page (DateTime)

March 14


Adeel booked a tour (DateTime)

Farhan write a note on the main page (DateTime)


YES I DO IT. with the help of Linq query. :

Solution:
1. I get my Notification data into listOfNotifications from database
2. Distinct Dates.
3. Create another Business entity named : Notification_Date (you can see B.E in the code)
4. In Notification_Date B.E compose Notifications B.E.
5. Loop through datesOnly and assign values to Notification_Date
6. Bind to Nested 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; }
}

OUTER DATALIST ITEMBOUND

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();
        }
}

DATALISTS in 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>

THIS IS THE OUTPUT:

how to show notifications categorized based on date

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜