如何遍历两个日期范围并在ASP.NET日历中显示? [英] How to iterate through two date ranges and display in ASP.NET Calendar?
本文介绍了如何遍历两个日期范围并在ASP.NET日历中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个酒店预订日历,选择 DateFrom
和 DateTo
我需要从 DateFrom
迭代到 DateTo
并显示所有这些日历中的日期,我到目前为止的代码只选择`DateFrom`和`DateTo`并将其作为标签显示在日历中:
I am trying to create a hotel booking calendar that selects the DateFrom
and DateTo
I need to iterate through from the DateFrom
to DateTo
and display all these dates in the calendar, the code I have so far only selects the `DateFrom` and `DateTo` and displays them in the calendar as a label:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
DateTime df = (DateTime)dr.Field<DateTime?>("DateFrom");
DateTime dt = (DateTime)dr.Field<DateTime?>("DateTo");
if (e.Day.Date == dt.Date)
{
Label lbl = new Label();
lbl.BackColor = System.Drawing.Color.Gray;
lbl.Text = "Booked From";
e.Cell.Controls.Add(lbl);
}
if (e.Day.Date == df.Date)
{
Label lbl = new Label();
lbl.BackColor = System.Drawing.Color.Gray;
lbl.Text = "Booked To";
e.Cell.Controls.Add(lbl);
}
}
推荐答案
我正在使用的解决方案,以防有需要的人来其中包括:
The solution I am using in case somebody who needs this comes across this is:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
DateTime df = (DateTime)dr.Field<DateTime?>("DateFrom");
DateTime dt = (DateTime)dr.Field<DateTime?>("DateTo");
if (e.Day.Date == dt.Date)
{
Label lbl = new Label();
lbl.BackColor = System.Drawing.Color.Gray;
lbl.Text = "Booked From";
e.Cell.Controls.Add(lbl);
}
if (e.Day.Date == df.Date)
{
Label lbl = new Label();
lbl.BackColor = System.Drawing.Color.Gray;
lbl.Text = "Booked To";
e.Cell.Controls.Add(lbl);
}
//Added Code
if(e.Day.Date > df.Date && e.Day.Date < dt.Date)
{
Label lbl = new Label();
lbl.BackColor = System.Drawing.Color.Gray;
lbl.Text = "Day inbetween";
e.Cell.Controls.Add(lbl);
}
}
这篇关于如何遍历两个日期范围并在ASP.NET日历中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文