2011-10-31 110 views
1

我想有一個日曆控件,只允許用戶選擇日期(週一至週五)的日期,如果它是在例如之後。當天上午9點,那麼這個日期也將不可用,然後返回到文本框中。ASP.net日曆控制條件

如何輕鬆做到這一點。我對ASP.net還很陌生。

謝謝

回答

1

更新時間:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
    { 
     if (e.Day.IsWeekend == true) 
     { 
      e.Cell.Enabled = false; 
      e.Day.IsSelectable = false; 
      e.Cell.BackColor = Color.Gray; 
     } 
     else 
     { 
      e.Cell.Enabled = true; 

      if (e.Day.Date == DateTime.Today) 
      { 
       // DateTime d = DateTime.Today; 
       if (DateTime.Now.Hour > 9) 
       { 
        e.Day.IsSelectable = false; 
        e.Cell.Enabled = false; 
        e.Cell.BackColor = Color.Gray; 
       } 
      } 
      else if (e.Day.Date < DateTime.Today) 
      { 
       e.Day.IsSelectable = false; 
       e.Cell.Enabled = false; 
       e.Cell.BackColor = Color.Gray; 
      } 

     } 

    } 

更新:

<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" onselectionchanged="Calendar1_SelectionChanged" 
      ></asp:Calendar> 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 




protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
     { 
      TextBox1.Text = Calendar1.SelectedDate.ToString(); 
     } 
+0

嗨,感謝您的回覆,我該如何調用Calendar_DayRender事件? – HL8

+0

嗨,感謝您的回覆,我該如何調用Calendar_DayRender事件?我是否將以下代碼放在aspx頁面中 如何添加文本框以插入所選日期用戶?謝謝 – HL8

+0

嗨我試過沒有工作,我有錯誤,System.EventArgs不包含Day和Cell的定義,並且當前上下文中不存在名稱Color。 – HL8

1

在處理事件的DayRender:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
{ 
    if (e.Day.IsWeekend == true) 
    { 
     e.Cell.Enabled = false; 
     e.Cell.BackColor = Color.Gray; 
    } 

    // add your other criteria as needed 
} 
1

的asp.net VB的,你可以這樣做:

Protected Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs) 
      If (e.Day.Date.DayOfWeek = DayOfWeek.Friday) Then 
       e.Day.IsSelectable = False 
       e.Cell.ForeColor = Drawing.Color.Red 
      End If 
      If e.Day.Date < System.DateTime.Today Then 
       e.Day.IsSelectable = False 
      End If 

End Sub 

這將禁用今天和週末關閉選項前的日期。