简体   繁体   English

ASP 日历不显示上个月和下个月的数据

[英]ASP Calendar not showing data for previous and next month

I have an ASP Calendar Page that pulls an event list from a database.我有一个从数据库中提取事件列表的 ASP 日历页面。 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?当单击不同的月份时,ASP 日历是否会重新加载整个页面,或者它是否充当查询字符串属性?

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

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.要查看是否使用了正确的月份,请将此行添加到GetSelectedMonthData方法。

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更确切地说,最好将AddWithValue更改为

comm.Parameters.Add("@FirstDate", SqlDbType.DateTime).Value = firstDate;
comm.Parameters.Add("@LastDate", SqlDbType.DateTime).Value = lastDate;

Does ASP Calendar reload the entire page when a different month is clicked or does it act as a query string property?当单击不同的月份时,ASP 日历是否会重新加载整个页面,或者它是否充当查询字符串属性?

Yes, the problem is the Postback, what you cannot avoid.是的,问题是回发,这是你无法避免的。

Since I have several calendars, I have made it general因为我有几个日历,所以我把它变成了通用的

Foo.aspx foo.aspx

  <asp:Calendar ID="cal1" runat="server" OnVisibleMonthChanged="cal_VisibleMonthChanged" [...]/>

  <asp:Calendar ID="cal2" runat="server" OnVisibleMonthChanged="cal_VisibleMonthChanged" [...]/>

  <asp:Calendar ID="cal3" runat="server" OnVisibleMonthChanged="cal_VisibleMonthChanged" [...]/>

Foo.aspx.cs foo.aspx.cs

protected void cal_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
    ((Calendar)sender).Visible = true;
}

This posting was helpful (for me) https://stackoverflow.com/a/25750717/15274506这篇文章很有帮助(对我来说) https://stackoverflow.com/a/25750717/15274506

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM