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

Display required month on RadCalender

1 Answer 140 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Anwar
Top achievements
Rank 2
Anwar asked on 16 Feb 2012, 10:26 AM
Hai friends,

We are using RadCalender in our application.
By default RadCalender displaying current month.
Our requirment is differ to this.

in our code we are having a variable selectDate(this variable contains date).
this selectDate may be less than current date or may be greater than the current date.

Now depending on the date ,we have to display respective month calender.
i.e
 if selectDate="24/2/2012"  ,then RadCalender has to display feb month
 if selectDate="24/3/2012"  ,then RadCalender has to display march month


Same requirement for RadScheduler also
 if selectDate="24/2/2012"  ,then RadScheduler has to display feb month
 if selectDate="24/3/2012"  ,then RadScheduler has to display march month


how can i do this

Thanks in advance
Anwar

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 20 Feb 2012, 05:17 PM
Anwar:

In order to have your RadCalendar instance display the month view based on your selected date variable, you simply need to set the RadCalendar instance .FocusedDate property to your selected date variable.

The FocusedDate property specifies the date that has focus. By default, RadCalendar uses the current date as the focused date. When the calendar shows more than one month (either in multi-view mode or with an enlarged day matrix), the focused date appears by default in the first month that the calendar shows. When the calendar is in multi-view mode, you can change which view contains the focused date by setting the FocusedDateColumn and FocusedDateRow properties.

Here's server-side code that sets RadCalendar1 Month View based on the RadDatePicker date input's current;y selected date when the "Apply on server" button gets clicked.:

Default.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI.Calendar.View;
 
 
public partial class Default : System.Web.UI.Page
{
        //private int DayOfMonth;
        private DateTime selectedDate;
        private string backgroundColor = String.Empty;
 
        private DateTime startDate;
        private DateTime endDate;
 
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RadDatePicker1.SelectedDate = DateTime.Today;
            }
                
            selectedDate = (DateTime)RadDatePicker1.SelectedDate;
            backgroundColor = ColorList.Items[ColorList.SelectedIndex].Value;
        }
 
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender (e);
 
            RadCalendar1.FocusedDate = selectedDate;
            startDate = ((MonthView) RadCalendar1.CalendarView).MonthStartDate;
            endDate = ((MonthView) RadCalendar1.CalendarView).MonthEndDate;
        }
 
        protected void CustomizeDay(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e)
        {
            DateTime CurrentDate = e.Day.Date;
            if (startDate <= CurrentDate && CurrentDate <= endDate)
            {
                if (CurrentDate == selectedDate)
                {
                    TableCell currentCell = e.Cell;
                    currentCell.Style["background-color"] = backgroundColor;
                }
            }
        }
}

See attached image.

Hope this helps!
Tags
Calendar
Asked by
Anwar
Top achievements
Rank 2
Answers by
Richard
Top achievements
Rank 1
Share this question
or