This is a migrated thread and some comments may be shown as answers.
Cannot change month in calendar
1 Answer 36 Views
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 06 Jan 2011, 10:23 PM
Hi

I've tried to add some custom text dynamically from code behind to the calendardays based on some different code examples. It's working fine on pageload and all events is displayed in the calendar for the current month. But I'm not able to change month and display events from other months. In fact i can only switch month one time ex: from jan -> feb.

I've tried a coupe of different code examples all with the same month-problem. Any idea what I'm doing wrong?

        protected override void OnInit(EventArgs e)
        {
            Hashtable eventHash = GetEventCollection(false);
            DateTime rcDate = RadCalendar1.CalendarView.ViewStartDate;
            while (rcDate <= RadCalendar1.CalendarView.ViewEndDate)
            {
                string key = rcDate.ToString("yyyy-MM-dd");
                if (eventHash.ContainsKey(key))
                {
                    List<Event> eventList = (List<Event>)eventHash[key];
                    CalendarDayTemplate template = new CalendarDayTemplate(RadCalendar1, rcDate, eventList.Count);
                }
 
                rcDate = rcDate.AddDays(1.0);
            }
            base.OnInit(e);
        }
 
public class CalendarDayTemplate : ITemplate
    {
        private Control cellContent = new Control();
        public CalendarDayTemplate(RadCalendar myCalendar, DateTime cellDate, int myData)
        {
            RadCalendarDay newDay = new RadCalendarDay();
            PlaceHolder myPlaceHolder = new PlaceHolder();
            Page myPage = new Page();
 
            HtmlGenericControl divContainer = new HtmlGenericControl("div");
            divContainer.Attributes.Add("class", "CalendarDayContainer");
 
            HtmlGenericControl divDate = new HtmlGenericControl("div");
            divDate.Attributes.Add("class", "Date");
            divDate.InnerText = cellDate.Day.ToString();
 
            divContainer.Controls.Add(divDate);
 
            HtmlGenericControl divEvent = new HtmlGenericControl("div");
            divEvent.Attributes.Add("class", "Event");
 
            if (myData > 0)
                divEvent.InnerText = string.Concat(myData, " events");
 
            divContainer.Controls.Add(divEvent);
 
            myPlaceHolder.Controls.Add(divContainer);
 
            this.cellContent = myPlaceHolder;
 
            // Create a DayTemplate with a unique ID 
            DayTemplate dayTemplate = new DayTemplate();
            dayTemplate.ID = cellDate.ToString("MMddyyyy");
            dayTemplate.Content = this;
 
            // create a SpecialDay, associate it with the DayTemplate and add to Calendar 
            newDay.Date = cellDate;
            newDay.TemplateID = dayTemplate.ID;
            myCalendar.SpecialDays.Add(newDay);
 
            myCalendar.CalendarDayTemplates.Add(dayTemplate);
        }
 
        // Once the RadCalendar is instantiated this will be called once for each cell
        public void InstantiateIn(Control container)
        {
            container.Controls.Add(this.cellContent);
        }
    }

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 10 Jan 2011, 04:30 PM
Hello Alex,

To solve this issue you need to move the logic in a later event. The OnInit event is too early and the calendar is not initialized properly that is why you cannot navigate between months. You can move this logic in the PreRender event for the page when the calendar is already initialized and you can safely configure day templates. Additionally you should clear the SpacialDays collection of the calendar before each initialization of the templates otherwise discrepancies occur in the count of CalendarDayTemplates and SpecialDays collections. Finally you need to set the AutoPostBack="true" for the calendar so that the templates are initialized on each postback after navigation through months.
Here is a sample code snippet illustrating the approach:

protected void Page_PreRender(object sender, EventArgs e)
    {
        Hashtable eventHash = GetEventCollection(false);
        DateTime rcDate = RadCalendar1.CalendarView.ViewStartDate;
        RadCalendar1.SpecialDays.Clear();
        while (rcDate <= RadCalendar1.CalendarView.ViewEndDate)
        {
            string key = rcDate.ToString("yyyy-MM-dd");
            if (eventHash.ContainsKey(key))
            {
                List<Event> eventList = (List<Event>)eventHash[key];
                CalendarDayTemplate template = new CalendarDayTemplate(RadCalendar1, rcDate, eventList.Count);
            }
  
            rcDate = rcDate.AddDays(1.0);
        }
    }

<telerik:RadCalendar ID="RadCalendar1" runat="server" AutoPostBack="true"/>


All the best,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Asked by
Alex
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or