Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two repeaters one nested inside the other.

The first repeater creates 12 rows, one for each month of the year. It works out the number of days for each month in turn and adds them to an array. The array is then used as the datasource for the second repeater.

The second repeater checks the days for each month and sets the background colour of a td if the DayOfWeek is either a Saturday or Sunday.

This all works correctly and does what I require.

What I need to do next is query a SQL database table that contains a list of holiday dates. The table contains the Start date of the holiday, the number of days taken and a hex colour code.

What I want it to do is find the start date in rptAbsenceDays_ItemDataBound and using the number of days and the colour value display it similar to the second link. This information is stored in hidden fields.

This first link is what I am trying to create: Shows holidays and weekends[^]

This link is what I have managed so far Only shows weekends[^]

How can I call the StaffAbsence and get it to display the holidays

What I have tried:

This is the code for the page

<div class="tab-pane fade" id="Absence">
    <div class="col-md-12">
        <table>
            <thead>
                <tr>
                    <th>
                        <asp:Literal ID="litYear" runat="server"></asp:Literal>
                    </th>
                    <%--Creates a row of 31 days along the top of the repeater--%>
                    <% for (int iWeek = 1; iWeek <= 31; iWeek++) { %>
                        <th style="width:30px; text-align:center; background-color:darkgray;"" >
                            <%= iWeek %>
                        </th>
                    <% } %>
                </tr>
            </thead>
            <asp:Repeater ID="rptAbsenceMonths" runat="server" OnItemDataBound="rptAbsenceMonths_ItemDataBound">
                <ItemTemplate>
                    <tr>
                    <td>
                        <asp:Literal ID="litMonth" runat="server" />
                    </td>
                        <asp:Repeater ID="rptAbsenceDays" runat="server" OnItemDataBound="rptAbsenceDays_ItemDataBound">
                            <ItemTemplate>
                                <td id="tdDay" runat="server">
                                    <asp:Literal ID="litDay" runat="server" />
                                    <asp:HiddenField ID="hidAbsenceStartDate" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ScheduleStartDate")%>'/>
                                    <asp:HiddenField ID="hidAbsenceTotalDays" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ScheduleDays")%>'/>
                                    <asp:HiddenField ID="hidAbsenceColour" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ScheduleColour")%>'/>
                                </td>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
    </div>
    <div class="clearfix"></div>

</div> <%--Absence--%>


Below is my C# code

protected void AbsenceMonths()
{
    litYear.Text = DateTime.Now.Year.ToString();
    rptAbsenceMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    rptAbsenceMonths.DataBind();

}


protected void rptAbsenceMonths_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptAbsenceDays = (Repeater)e.Item.FindControl("rptAbsenceDays");
        Literal litMonth = (Literal)e.Item.FindControl("litMonth");
        _currentBindingMonth = (int)e.Item.DataItem;
        _absenceMonth = new DateTime(DateTime.Now.Year, _currentBindingMonth, 1);
        litMonth.Text = _absenceMonth.ToString("MMM");

        int numberOfDays = System.DateTime.DaysInMonth(Convert.ToInt16(litYear.Text), _currentBindingMonth);
        DateTime[] monthdays = new DateTime[numberOfDays];
        for (int i = 0; i < numberOfDays; i++)
        {
            monthdays[i] = _absenceMonth;
            _absenceMonth = _absenceMonth.AddDays(1);
        }

        rptAbsenceDays.DataSource = monthdays;
        rptAbsenceDays.DataBind();

    }

}


protected void rptAbsenceDays_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DateTime date = (DateTime)e.Item.DataItem;
        Literal litDay = (Literal)e.Item.FindControl("litDay");
        HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");

        if(date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
        {
            tdDay.Style.Add("background-color", "#D0D0D0");
        }

        tdDay.ID = string.Empty;
    }
}


protected void StaffAbsence(int StaffID, int AbsenceYear)
{
    SqlCommand cmd = new SqlCommand("sp_GetStaffAbsence", cnn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@EmpCode", StaffID);
    cmd.Parameters.AddWithValue("@Year", AbsenceYear);
    DataTable dt = new DataTable();
    SqlDataAdapter adapt = new SqlDataAdapter(cmd);
    adapt.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        //rptAbsenceDays.DataSource = dt;
        //rptAbsenceDays.DataBind();
        //rptAbsenceDays.Visible = true;
    }

}


Any help much appreciated.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900