Monday, 5 August 2013

Select multiple date with limited range date selection from Ajax Calender control and show these in gridview in asp.net

.aspx contents:

<asp:Calendar ID="Calendar1" runat="server" BackColor="#CCF0C8" BorderColor="White"
           Font-Underline="false" BorderWidth="1px" Font-Names="Calibri" Font-Size="9pt"
           Font-Bold="true" ForeColor="Black" Height="155px" NextPrevFormat="ShortMonth"
           OnPreRender="Calendar1_PreRender" Width="170px" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" BackColor="#B4C86F" />
   <NextPrevStyle Font-Bold="True" Font-Size="12px" ForeColor="#F5E9CB" VerticalAlign="Bottom" />
    <OtherMonthDayStyle ForeColor="#999999" />
     <SelectedDayStyle BackColor="Maroon" ForeColor="White" />
     <TitleStyle BackColor="#227E15" BorderColor="White" BorderWidth="2px" Font-Bold="True"
                                                                                Font-Size="10pt" ForeColor="White" />

   </asp:Calendar>


<asp:GridView ID="GridView1" Font-Names="Calibri" Font-Size="Small" runat="server"
          EmptyDataText="No Selected Dates" CellPadding="4" AutoGenerateColumns="false"
          ForeColor="#333333" GridLines="None" Width="150px">
         <AlternatingRowStyle BackColor="#C4F9BD" />
        <HeaderStyle BackColor="#227E15" Font-Bold="True" ForeColor="White" Font-Size="14px" />
         <RowStyle BackColor="#EFF3FB" />
         <Columns>
         <asp:TemplateField HeaderText="Selected Date" ItemStyle-HorizontalAlign="Center">
               <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%#                                                     DateTime.Parse(Container.DataItem.ToString()).ToString("yyyy-MM-dd") %>' ></asp:Label>
               </ItemTemplate>
          <ItemStyle HorizontalAlign="Center" Font-Bold="true" />
           </asp:TemplateField>
           </Columns>
           </asp:GridView>


C# code:

        public List<DateTime> SelectedDates
        {
            get
            {
                if (ViewState["Dates"] == null)
                    ViewState["Dates"] = new List<DateTime>() { DateTime.MaxValue.AddDays(-2) };
                return (List<DateTime>)ViewState["Dates"];
            }
            set
            {
                ViewState["Dates"] = value;
            }
        }
        protected void Calendar1_PreRender(object sender, EventArgs e)
        {
            // Reset Selected Dates
            Calendar1.SelectedDates.Clear();
            // Select previously Selected Dates
            foreach (DateTime dt in SelectedDates)
                Calendar1.SelectedDates.Add(dt);
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {


            if (SelectedDates.Contains(Calendar1.SelectedDate))
                SelectedDates.Remove(Calendar1.SelectedDate);
            else
                SelectedDates.Add(Calendar1.SelectedDate);
            //update selection    
            GridView1.Visible = true;
            ViewState["Dates"] = SelectedDates;
            GridView1.DataSource = SelectedDates.Skip(1);
            GridView1.DataBind();
         
        }


        protected void Calendar1_DayRender(Object sender, DayRenderEventArgs e)
        {
         
            // Max day is to be the day before today, so subtract one day.
            DateTime maxDate = DateTime.Now.AddDays(ld);
            maxDate.AddDays(-1);
            if ((e.Day.Date > maxDate) || e.Day.Date.CompareTo(DateTime.Today.AddDays(-1)) < 0)
            {
                // Only make dates that are in our range selectable:
                e.Day.IsSelectable = false;
                e.Cell.ForeColor = Color.Gray;

            }


        }