【问题标题】:ASP Calendar not showing data for previous and next monthASP 日历不显示上个月和下个月的数据
【发布时间】:2018-08-29 12:16:17
【问题描述】:

我有一个从数据库中提取事件列表的 ASP 日历页面。我遇到的问题是当我点击下个月和上个月时,显示的是当月的数据,而不是上个月和下个月的数据。当单击不同的月份时,ASP 日历是否会重新加载整个页面,还是充当查询字符串属性?

这是我目前使用的代码:

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

【问题讨论】:

  • 您是否有可能在页面级别使用EnableViewState="false"Calendar 控件上的ViewStateMode="Disabled" 运行?

标签: c# asp.net webforms


【解决方案1】:

我测试了你的整个代码。它工作得很好。但在第一次加载时,它会执行两次代码。

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

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

要查看是否使用了正确的月份,请将此行添加到 GetSelectedMonthData 方法。

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

如果显示的月份正确,则很可能是数据库中的日期有问题。但只是为了确保...您确实将事件添加到日历控件?

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

为了更加确定,最好将AddWithValue改为

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

【讨论】:

  • 我使用了你的 onVisibleMonthChanged 建议,当我点击上个月或下个月时,它显示为 1/1/0001,这是一个空值。
  • 调试代码,看看为什么会出错。就像我在测试它时所说的那样。如果您不知道如何在 Visual Studio 中调试,您可以使用该技巧和标签来查看哪里出错了。但话又说回来,它从日历本身获取日期,所以奇怪的是它们是错误的......
  • 取得了任何进展?
【解决方案2】:

当单击不同的月份时,ASP 日历是否会重新加载整个页面,还是充当查询字符串属性?

是的,问题是回发,这是你无法避免的。

因为我有几个日历,所以我把它通用了

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

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

这篇文章很有帮助(对我来说) https://stackoverflow.com/a/25750717/15274506

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多