[英]ASP Calendar not showing data for previous and next month
我有一個從數據庫中提取事件列表的 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 = " ";
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 = " ";
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();
}
我測試了你的整個代碼。 它工作得很好。 但是在第一次加載時它會執行兩次代碼。
//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;
當單擊不同的月份時,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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.