Saturday, 22 September 2012

Beginning .Net : Multiple selection of date in asp.net calendar control with C# Examples and VB.Net Examples

Click Here to Download Sample MultipleDateSelectionInCalendarControl.zip

You can select multiple dates in calendar control.
There is a no direct way to select multiple dates in calendar control. You need to write some code for that.

Here is example for this.
In this example we take one calendar control and select multiple dates in that control and we display that selected dates in label.

ASPX Code : 
      <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black" 
            Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="250px"
                NextPrevFormat="ShortMonth" OnPreRender="Calendar1_PreRender" Width="330px" 
                OnSelectionChanged="Calendar1_SelectionChanged" BorderStyle="Solid" 
            CellSpacing="1">
                <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" 
                    Height="8pt" />
                <DayStyle BackColor="#CCCCCC" />
                <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
                <OtherMonthDayStyle ForeColor="#999999" />
                <SelectedDayStyle BackColor="#333399" ForeColor="White" />
                <TitleStyle BackColor="#333399" Font-Bold="True"
                    Font-Size="12pt" ForeColor="White" BorderStyle="Solid" Height="12pt" />
                <TodayDayStyle BackColor="#999999" ForeColor="White" />
        </asp:Calendar>

         <br />
         <br />
         <asp:Button runat="server" ID="btnGetSelectedDate" Text="Get Selected Date" 
            onclick="btnGetSelectedDate_Click" /> &nbsp;&nbsp;&nbsp;<br />
         <b>Selected Dates : </b><asp:Label runat="server" ID="lblDate" ></asp:Label>


C# Examples :
    protected void Calendar1_PreRender(object sender, EventArgs e)
    {
        
        Calendar1.SelectedDates.Clear();

        foreach (DateTime dt in MultipleSelectedDates)
        {
            Calendar1.SelectedDates.Add(dt);
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {

        if (MultipleSelectedDates.Contains(Calendar1.SelectedDate))
        {
            MultipleSelectedDates.Remove(Calendar1.SelectedDate);
        }
        else
        {
            MultipleSelectedDates.Add(Calendar1.SelectedDate);
        }
        
        ViewState["MultipleSelectedDates"] = MultipleSelectedDates;
    }
   

    public List<DateTime> MultipleSelectedDates
    {
        get
        {
            if (ViewState["MultipleSelectedDates"] == null)

                ViewState["MultipleSelectedDates"] = new List<DateTime>() ;
            return (List<DateTime>)ViewState["MultipleSelectedDates"];
        }
        set
        {
            ViewState["MultipleSelectedDates"] = value;
        }
    }


    protected void btnGetSelectedDate_Click(object sender, EventArgs e)
    {

        foreach (DateTime dt in MultipleSelectedDates)
        {
            lblDate.Text = lblDate.Text + " <br/> " + dt.ToString("dd/MM/yyyy");
        }
    }

VB.net Examples :
    Protected Sub Calendar1_PreRender(ByVal sender As Object, ByVal e As EventArgs)

        Calendar1.SelectedDates.Clear()

        For Each dt As DateTime In MultipleSelectedDates
            Calendar1.SelectedDates.Add(dt)
        Next
    End Sub
    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)

        If MultipleSelectedDates.Contains(Calendar1.SelectedDate) Then
            MultipleSelectedDates.Remove(Calendar1.SelectedDate)
        Else
            MultipleSelectedDates.Add(Calendar1.SelectedDate)
        End If

        ViewState("MultipleSelectedDates") = MultipleSelectedDates
    End Sub


    Public Property MultipleSelectedDates() As List(Of DateTime)
        Get
            If ViewState("MultipleSelectedDates") Is Nothing Then

                ViewState("MultipleSelectedDates") = New List(Of DateTime)()
            End If
            Return DirectCast(ViewState("MultipleSelectedDates"), List(Of DateTime))
        End Get
        Set(ByVal value As List(Of DateTime))
            ViewState("MultipleSelectedDates") = value
        End Set
    End Property

    Protected Sub btnGetSelectedDate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetSelectedDate.Click
        For Each dt As DateTime In MultipleSelectedDates
            lblDate.Text = lblDate.Text + " <br/> " & dt.ToString("dd/MM/yyyy")
        Next
    End Sub

Output : 


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


4 comments:

  1. Good stuff.. nice and easy to read and use..
    Thanks :)

    ReplyDelete
  2. That is so GREAT! Nice way in so few codes to resolve many doubts!

    ReplyDelete
  3. Do you know what the best way to create the list as a comma separated label? Either way I concatenate I get either a leading or trailing comma.

    lblDate2.Text = lblDate2.Text + dt.ToString("dd/MM/yyyy") + " ,";

    ReplyDelete