【问题标题】:Persisting date selections when visible month changes可见月份更改时保留日期选择
【发布时间】:2011-02-21 09:16:55
【问题描述】:
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

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

谢谢

【问题讨论】:

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


    【解决方案1】:

    在这种情况下,获得我想要的结果的最快方法是放下轮子和凿子,并使用来自 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获取日历控件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多