Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP Calendar not showing data for previous and next month

I have an ASP Calendar Page that pulls an event list from a database. The issue that I am having is that data is shown for the current month but not for the previous and next months when I click on the next and previous months. Does ASP Calendar reload the entire page when a different month is clicked or does it act as a query string property?

Here is the code that I am currently using:

protected DataSet dsEvents;

protected void Page_Load(object sender, EventArgs e)
{
    Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
    Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;
    Calendar1.TitleFormat = TitleFormat.Month;
    Calendar1.ShowGridLines = true;
    Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left;
    Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top;
    Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.LightGray;

    Calendar1.VisibleDate = DateTime.Today;
    FillEventDataset();
    GetFirstDayOfNextMonth();

    if (!IsPostBack)
    {
        Calendar1.VisibleDate = DateTime.Today;
        FillEventDataset();
        GetFirstDayOfNextMonth();
    }
}

protected DateTime GetFirstDayOfNextMonth()
{
    int monthNumber, yearNumber;
    if (Calendar1.VisibleDate.Month == 12)
    {
        monthNumber = 1;
        yearNumber = Calendar1.VisibleDate.Year + 1;
    }
    else
    {
        monthNumber = Calendar1.VisibleDate.Month + 1;
        yearNumber = Calendar1.VisibleDate.Year;
    }
    DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
    return lastDate;
}

protected void FillEventDataset()
{
    DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, 1);
    DateTime lastDate = GetFirstDayOfNextMonth();
    dsEvents = GetSelectedMonthData(firstDate, lastDate);
}

protected DataSet GetSelectedMonthData(DateTime firstDate, DateTime lastDate)
{
    DataSet dsMonth = new DataSet();

    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        SqlCommand comm = new SqlCommand("SELECT EventDate, EventLocation, EventSubject, EventStart FROM EventList WHERE EventDate >= @FirstDate AND EventDate <= @LastDate", conn);

        comm.CommandType = CommandType.Text;
        comm.Parameters.AddWithValue("@FirstDate", firstDate);
        comm.Parameters.AddWithValue("@LastDate", lastDate);

        conn.Open();
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(comm);
        try
        {
            sqlDataAdapter.Fill(dsMonth);
        }
        finally
        {
            conn.Close();
        }
    }
    return dsMonth;
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    e.Day.IsSelectable = false;
    e.Cell.ForeColor = System.Drawing.Color.Black;
    if (e.Day.IsOtherMonth)
    {
        e.Cell.Visible = true;
        e.Cell.Text = "";
    }

    DateTime nextEvent;
    String nextLocation;
    String nextSubject;
    String nextStart;

    if (dsEvents != null)
    {
        foreach (DataRow dr in dsEvents.Tables[0].Rows)
        {

            nextEvent = (DateTime)dr["EventDate"];
            nextLocation = dr["EventLocation"].ToString();
            nextSubject = dr["EventSubject"].ToString();
            nextStart = dr["EventStart"].ToString();

            if (nextEvent == e.Day.Date)
            {
                Literal literal1 = new Literal();
                literal1.Text = "<br/>";
                e.Cell.Controls.Add(literal1);

                Label label1 = new Label();
                label1.Text = nextStart.ToString();
                label1.Font.Size = new FontUnit(FontSize.Small);
                e.Cell.Controls.Add(label1);

                Literal literal2 = new Literal();
                literal2.Text = "&nbsp;&nbsp;";
                e.Cell.Controls.Add(literal2);

                Label label2 = new Label();
                label2.Text = nextSubject.ToString();
                label2.Font.Size = new FontUnit(FontSize.Small);
                e.Cell.Controls.Add(label2);

                Literal literal3 = new Literal();
                literal3.Text = "&nbsp;&nbsp;";
                e.Cell.Controls.Add(literal3);

                Label label3 = new Label();
                label3.Text = nextLocation.ToString();
                label3.Font.Size = new FontUnit(FontSize.Small);
                e.Cell.Controls.Add(label3);
            }

        }
    }
}

protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
    GetFirstDayOfNextMonth();
    FillEventDataset();
}
like image 598
Dan Nick Avatar asked Aug 29 '18 12:08

Dan Nick


1 Answers

I tested your entire code. It works just fine. But at first load it executes the code twice.

//remove these two lines
FillEventDataset();
GetFirstDayOfNextMonth();

if (!IsPostBack)
{
    Calendar1.VisibleDate = DateTime.Today;
    FillEventDataset();
    GetFirstDayOfNextMonth();
}

To view if the correct months are used, add this line to the GetSelectedMonthData method.

Label1.Text = firstDate.ToLongDateString() + " - " + lastDate.ToLongDateString();

If the months displayed there are correct there is most likely a problem with the dates in the database. But just to make sure... You did add the events to the Calendar Control?

<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender"
  OnVisibleMonthChanged="Calendar1_VisibleMonthChanged"></asp:Calendar>

To be extra sure, it is better to change AddWithValue to

comm.Parameters.Add("@FirstDate", SqlDbType.DateTime).Value = firstDate;
comm.Parameters.Add("@LastDate", SqlDbType.DateTime).Value = lastDate;
like image 192
VDWWD Avatar answered Oct 21 '22 02:10

VDWWD