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.


19 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
  4. This code vb.net display double date

    ReplyDelete
  5. soo good! thanks

    ReplyDelete
  6. one day problem.one day select after select not deselected

    ReplyDelete
  7. I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.
    Dot Net Training in Chennai

    ReplyDelete
  8. Hi, thanks for sharing such an informative blog. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update.

    weblogic tutorial

    ReplyDelete
  9. Thanks For Sharing Such an Important and Useful Content On Mulesoft Certification Training

    ReplyDelete
  10. Really Thanks for Posting Such an Useful Stuff...

    JNTU 99

    ReplyDelete
  11. Learn Digital Academy offers, Digital marketing training in bangalore
    It involves Training, Certification, Internships,and Placement.

    ReplyDelete
  12. How can I write the dates but separeted by the month ex. 1,2,3 of July , 2,4,5 of August

    ReplyDelete
  13. Existing without the answers to the difficulties you’ve sorted out
    through this guide is a critical case, as well as the kind which could
    have badly affected my entire career if I had not discovered your
    website.

    ReplyDelete