2009年4月25日 星期六

Calendar 控制項日期複選

Calendar 控制項可以透過 SelectedDate 屬性來取目前選取的日期,不過它沒有提供日期複選的功能。為達到日期複選的功能,我們在 DayRender 事件中,為每一個日期儲存格加入一個 HtmlInputCheckBox 控制項做日期選取,當 PostBack 時再透過 Request.Form 來取得用戶端複選的日期。

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
Dim oCheckBox As New HtmlControls.HtmlInputCheckBox()

oCheckBox.Value = e.Day.Date.ToShortDateString
oCheckBox.ID = "SelectDate"
e.Cell.Controls.Clear()
e.Cell.Controls.Add(oCheckBox)
e.Cell.Controls.Add(New LiteralControl(e.Day.DayNumberText))
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Response.Write("SelectDate: " & Me.Request.Form("SelectDate"))
End Sub
End Class
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
Dim oCheckBox As New HtmlControls.HtmlInputCheckBox()

oCheckBox.Value = e.Day.Date.ToShortDateString
oCheckBox.ID = "SelectDate"
e.Cell.Controls.Clear()
e.Cell.Controls.Add(oCheckBox)
e.Cell.Controls.Add(New LiteralControl(e.Day.DayNumberText))
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Response.Write("SelectDate: " & Me.Request.Form("SelectDate"))
End Sub
End Class
執行程式,勾選了6/2、6/10、6/18 三個日期。

當按下按鈕產生 PostBack 時,透過 Request.Form 即可取得複選的日期。

沒有留言:

張貼留言