0

我正在尝试创建一个选择DateFromDateTo

DateFrom我需要从to遍历DateTo并在日历中显示所有这些日期,到目前为止我的代码只选择DateFromandDateTo并将它们作为标签显示在日历中:

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

2 回答 2

1

这是一个简单的解决方案。

DateTime temp = df;

while (temp < dateTo)
{
   temp = temp.AddDays(1);
   // do something with inbetween date here...
}
于 2012-12-19T14:36:00.950 回答
1

不改变你的代码太多我会这样做:

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);
    }
} 
于 2012-12-19T14:40:11.733 回答