This is a migrated thread and some comments may be shown as answers.

RadDatepicker Disabling dates and changing disabled dates on postback

2 Answers 168 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Katie
Top achievements
Rank 1
Katie asked on 03 Jul 2012, 12:16 PM
I need to be able to have a calendar which only allows the user to select a saturday if a 7 nights is chosen from a combo box on a form and to allow the user to just select a monday if an 4 nights is chosen from the combo box.

I am currently using the RadDatepicker and have set it up to add the disabled dates for the year to the special days. So if a 7 is selected the dt variable will be every day except a saturday.
RadCalendarDay holiday = new RadCalendarDay();
        holiday.Date = dt;
        holiday.IsSelectable = false;
        holiday.IsDisabled = true;
        CalendarPopup1.Calendar.SpecialDays.Add(holiday);

I don't seem to be able to get this to work. I am also trying to reset the calendar whenever the combo box value changes. But can't get this to work either.
public void ResetCalendar()
{
    CalendarPopup1.Clear();
    CalendarPopup1.Calendar.SpecialDays.Clear();
}

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Jul 2012, 01:03 PM
Hi Katie,

Here is the sample code that I tried based on your scenario.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
  <Items>
     <telerik:RadComboBoxItem Text="1" />
     <telerik:RadComboBoxItem Text="2" />
     <telerik:RadComboBoxItem Text="3" />
     <telerik:RadComboBoxItem Text="4" />
     <telerik:RadComboBoxItem Text="5" />
     <telerik:RadComboBoxItem Text="6" />
     <telerik:RadComboBoxItem Text="7" />
  </Items>
</telerik:RadComboBox>
<telerik:RadDatePicker ID="RadDatePicker1" runat="server">
</telerik:RadDatePicker>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />

C#:
protected void RadComboBox1_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (RadComboBox1.SelectedItem.Text == "7")
        {
            RadDatePicker1.Calendar.DayRender += new Telerik.Web.UI.Calendar.DayRenderEventHandler(Calendar_DayRender);
        }
        else if(RadComboBox1.SelectedItem.Text=="4")
        {
            RadDatePicker1.Calendar.DayRender += new Telerik.Web.UI.Calendar.DayRenderEventHandler(Calendar_DayRender);
        }
    }
void  Calendar_DayRender(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e)
    {
        if (RadComboBox1.SelectedItem.Text == "7")
        {
            if (e.Day.Date.DayOfWeek == DayOfWeek.Saturday)
            {
                 RadCalendarDay holiday = new RadCalendarDay();
                holiday.Date = e.Day.Date;
                holiday.IsSelectable = false;
                holiday.IsDisabled = true;
                RadDatePicker1.Calendar.SpecialDays.Add(holiday);
            }
        }
        else if (RadComboBox1.SelectedItem.Text == "4")
        {
            if (e.Day.Date.DayOfWeek == DayOfWeek.Monday)
            {
                RadCalendarDay holiday = new RadCalendarDay();
                holiday.Date = e.Day.Date;
                holiday.IsSelectable = false;
                holiday.IsDisabled = true;
                RadDatePicker1.Calendar.SpecialDays.Add(holiday);
            }
        }
    }
protected void Button1_Click(object sender, EventArgs e)
    {
        RadDatePicker1.Clear();
        RadDatePicker1.Calendar.SpecialDays.Clear();
    }

Hope this helps.

Thanks.
Princy.
0
Katie
Top achievements
Rank 1
answered on 05 Jul 2012, 07:23 AM
Thank you, that really helped, I have now got it working.
Tags
Calendar
Asked by
Katie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Katie
Top achievements
Rank 1
Share this question
or