Week wise Grid: Display days of week in Repeater / GridView in ASP.Net

Last Reply on Oct 08, 2014 07:18 AM By Azim

Posted on Oct 08, 2014 03:03 AM

How to show current date along with six days as a week in html header with navigation bar for next week and previous week?

Posted on Oct 08, 2014 07:18 AM Modified on on Oct 08, 2014 10:18 AM

Please refer this code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        table
        {
            border: 1px solid #ccc;
        }
        table th
        {
            background-color: #F7F7F7;
            color: #333;
            font-weight: bold;
        }
        table th, table td
        {
            padding: 5px;
            border-color: #ccc;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <th>
                            <asp:Label ID="lblDay1" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 1).ToShortDateString()%>'
                                runat="server" />
                        </th>
                        <th>
                            <asp:Label ID="lblDay2" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 2).ToShortDateString()%>'
                                runat="server" />
                        </th>
                        <th>
                            <asp:Label ID="lblDay3" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 3).ToShortDateString()%>'
                                runat="server" />
                        </th>
                        <th>
                            <asp:Label ID="lblDay4" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 4).ToShortDateString()%>'
                                runat="server" />
                        </th>
                        <th>
                            <asp:Label ID="lblDay5" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 5).ToShortDateString()%>'
                                runat="server" />
                        </th>
                        <th>
                            <asp:Label ID="lblDay6" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 6).ToShortDateString()%>'
                                runat="server" />
                        </th>
                        <th>
                            <asp:Label ID="lblDay7" Text='<%#DateTime.Now.AddDays(Container.ItemIndex + 7).ToShortDateString()%>'
                                runat="server" />
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox5" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox6" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox7" runat="server" />
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        <br />
        <asp:Button Text="Previous" OnClick="Previous" runat="server" />
        &nbsp;
        <asp:Button Text="Next" OnClick="Next" runat="server" />
    </div>
    </form>
</body>
</html>

Namespace

using System.Data;

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[7] {
    new DataColumn("Day1",typeof(DateTime)),
    new DataColumn("Day2",typeof(DateTime)),
    new DataColumn("Day3",typeof(DateTime)),
    new DataColumn("Day4",typeof(DateTime)),
    new DataColumn("Day5",typeof(DateTime)),
    new DataColumn("Day6",typeof(DateTime)),
    new DataColumn("Day7",typeof(DateTime))});
        dt.Rows.Add();
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }
}

protected void Next(object sender, EventArgs e)
{

    Label day1 = Repeater1.Controls[0].Controls[0].FindControl("lblDay1") as Label;
    Label day2 = Repeater1.Controls[0].Controls[0].FindControl("lblDay2") as Label;
    Label day3 = Repeater1.Controls[0].Controls[0].FindControl("lblDay3") as Label;
    Label day4 = Repeater1.Controls[0].Controls[0].FindControl("lblDay4") as Label;
    Label day5 = Repeater1.Controls[0].Controls[0].FindControl("lblDay5") as Label;
    Label day6 = Repeater1.Controls[0].Controls[0].FindControl("lblDay6") as Label;
    Label day7 = Repeater1.Controls[0].Controls[0].FindControl("lblDay7") as Label;
    DateTime dtime1 = Convert.ToDateTime(day1.Text);
    DateTime dtime2 = Convert.ToDateTime(day2.Text);
    DateTime dtime3 = Convert.ToDateTime(day3.Text);
    DateTime dtime4 = Convert.ToDateTime(day4.Text);
    DateTime dtime5 = Convert.ToDateTime(day5.Text);
    DateTime dtime6 = Convert.ToDateTime(day6.Text);
    DateTime dtime7 = Convert.ToDateTime(day7.Text);
    day1.Text = dtime1.AddDays(1).ToShortDateString();
    day2.Text = dtime2.AddDays(1).ToShortDateString();
    day3.Text = dtime3.AddDays(1).ToShortDateString();
    day4.Text = dtime4.AddDays(1).ToShortDateString();
    day5.Text = dtime5.AddDays(1).ToShortDateString();
    day6.Text = dtime6.AddDays(1).ToShortDateString();
    day7.Text = dtime7.AddDays(1).ToShortDateString();
}

protected void Previous(object sender, EventArgs e)
{

    Label day1 = Repeater1.Controls[0].Controls[0].FindControl("lblDay1") as Label;
    Label day2 = Repeater1.Controls[0].Controls[0].FindControl("lblDay2") as Label;
    Label day3 = Repeater1.Controls[0].Controls[0].FindControl("lblDay3") as Label;
    Label day4 = Repeater1.Controls[0].Controls[0].FindControl("lblDay4") as Label;
    Label day5 = Repeater1.Controls[0].Controls[0].FindControl("lblDay5") as Label;
    Label day6 = Repeater1.Controls[0].Controls[0].FindControl("lblDay6") as Label;
    Label day7 = Repeater1.Controls[0].Controls[0].FindControl("lblDay7") as Label;
    DateTime dtime1 = Convert.ToDateTime(day1.Text);
    DateTime dtime2 = Convert.ToDateTime(day2.Text);
    DateTime dtime3 = Convert.ToDateTime(day3.Text);
    DateTime dtime4 = Convert.ToDateTime(day4.Text);
    DateTime dtime5 = Convert.ToDateTime(day5.Text);
    DateTime dtime6 = Convert.ToDateTime(day6.Text);
    DateTime dtime7 = Convert.ToDateTime(day7.Text);
    day1.Text = dtime1.AddDays(-1).ToShortDateString();
    day2.Text = dtime2.AddDays(-1).ToShortDateString();
    day3.Text = dtime3.AddDays(-1).ToShortDateString();
    day4.Text = dtime4.AddDays(-1).ToShortDateString();
    day5.Text = dtime5.AddDays(-1).ToShortDateString();
    day6.Text = dtime6.AddDays(-1).ToShortDateString();
    day7.Text = dtime7.AddDays(-1).ToShortDateString();
}

Screenshot