可见月份更改时保留日期选择

时间:2011-02-21 09:16:56

标签: c# asp.net vb.net datetime calendar

Dim List As New List(Of DateTime)

然后在我的按钮点击事件:

 If InputBookinglength.SelectedValue.ToString = "2" Then 
            Dim paramstring As New StringBuilder
            If Session("SelectedDates") IsNot Nothing Then
                Dim newList As List(Of DateTime) = DirectCast(Session("SelectedDates"), List(Of DateTime))

                For Each dt As DateTime In newList
                    paramstring.Append(dt.ToShortDateString() & " - ")
                Next
            End If
  command.Parameters.AddWithValue("@multibookingdates", paramstring.tostring)

然后我有:

  Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If e.Day.IsSelected = True Then
            List.Add(e.Day.[Date])
        End If
        Session("SelectedDates") = List
    End Sub


Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        If Session("SelectedDates") IsNot Nothing Then
            Dim newList As List(Of DateTime) = DirectCast(Session("SelectedDates"), List(Of DateTime))

            For Each dt As DateTime In newList
                Calendar1.SelectedDates.Add(dt)
            Next
            List.Clear()
        End If
    End Sub

    Protected Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged
        If Session("SelectedDates") IsNot Nothing Then
            Dim newList As List(Of DateTime) = DirectCast(Session("SelectedDates"), List(Of DateTime))

            For Each dt As DateTime In newList
                Calendar1.SelectedDates.Add(dt)
            Next
            List.Clear()
        End If
    End Sub
End Class

在一个月内选择多天时,此代码可以正常工作。但是当您切换到显示不同的月份时,前几个月的选择将丢失。如果可见月份发生变化,请告诉我如何保留选择。

由于

1 个答案:

答案 0 :(得分:0)

在这种情况下,获得我想要的结果的最快方法是放下方向盘和凿子并使用来自obout的第三方控件(日历控件)。

然后捕获多个日期就像设置多选日期一样简单:

 <obout:Calendar ID="MultiCalendar" runat="server" MultiSelectedDates="true" 
    Columns="1" CultureName="en-GB" TitleText="" ShowMonthSelector="true" 
    MonthSelectorType="DropDownList" >
    </obout:Calendar>

然后我可以得到像这样的值:

 Dim dateString As New StringBuilder
            For Each item As String In MultiCalendar.SelectedDates
                dateString.Append(item)
                dateString.Append(" - ")
            Next
            command.Parameters.AddWithValue("@multibookingdates", dateString.ToString)

您可以从http://www.obout.com

获取日历控件