More in this section

Forums / Developing with Sitefinity / radcalendar and eventsview

radcalendar and eventsview

6 posts, 0 answered
  1. Rabindra
    Rabindra avatar
    106 posts
    Registered:
    17 May 2009
    15 Aug 2009
    Link to this post
    When I click any date on Radcalendar, I want to show events for that date.
    Can you please give me code samples?
    thanks
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    15 Aug 2009
    Link to this post
    Hi Rabindra,

    You can take a look at the following post where a solution was provided. Below is another more complicated sample code:

    using System; 
    using System.Collections.Generic; 
    using Telerik.Cms.Web.UI; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.ComponentModel; 
    using Telerik.Cms.Engine.WebControls; 
    using System.Collections; 
    using Telerik.Cms.Engine; 
    using System.Globalization; 
    using Telerik.Web.UI; 
    using Telerik.Framework.Web; 
    using Telerik.Web; 
    using Telerik.Events; 
     
     
        public class EventsCalendar : ContentBasic 
        { 
            #region Properties 
            [WebEditor("Telerik.FileManager.UrlWebEditor, Telerik.FileManager"), Category("Appearance"), Themeable(false), DefaultValue("")] 
            [WebSysTemplate(EventsCalendar.EventsCalendarTemplateName, "EventsCalendar_Desc""/Events"true"2009-02-01")] 
             
             
            public virtual string EventsTemplatePath 
            { 
                get 
                { 
                    return this.layoutTemplatePath; 
                } 
                set 
                { 
                    this.layoutTemplatePath = value; 
                } 
            } 
     
            protected virtual string LayoutTemplateName 
            { 
                get 
                { 
                    return EventsCalendar.EventsCalendarTemplateName; 
                } 
            } 
     
            [Browsable(false)] 
            [DefaultValue((string)null)] 
            [TemplateContainer(typeof(EventsCalendar), BindingDirection.TwoWay)] 
            [PersistenceMode(PersistenceMode.InnerProperty)] 
            public virtual ITemplate EventsTemplate 
            { 
                get 
                { 
                    if (this.layoutTemplate == null
                        this.layoutTemplate = ControlUtils.GetTemplate(this.EventsTemplatePath, this.LayoutTemplateName, 
                                                                       this.GetType(), this.GetType(), this.GetType(), this.ProviderName); 
                    return this.layoutTemplate; 
                } 
                set 
                { 
                    this.layoutTemplate = value; 
                } 
            } 
     
            private DateTime? CurrentViewDate 
            { 
                get 
                { 
                    try 
                    { 
                        DateTime? currendDate = DateTime.Today; 
     
                        string month = this.Context.Request["Month"]; 
                        string year = this.Context.Request["Year"]; 
                        string day = this.Context.Request["Day"]; 
     
                        if (string.IsNullOrEmpty(month) || string.IsNullOrEmpty(day) || string.IsNullOrEmpty(year)) 
                        { 
                        } 
                        else 
                        { 
                            currendDate = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day)); 
                        } 
     
                        return currendDate; 
                    } 
                    catch 
                    { 
                        // do nothing.; 
                    } 
                    return null
                } 
     
            protected override string ProviderNameImpl 
            { 
                get 
                { 
                    return EventsManager.DefaultContentProvider; 
                } 
            } 
     
            protected virtual GenericContainer Container 
            { 
                get 
                { 
                    if (this.container == null
                    { 
                        this.container = new GenericContainer(); 
                        this.EventsTemplate.InstantiateIn(this.container); 
                    } 
                    return this.container; 
                } 
            } 
            #endregion 
            #region Control references 
     
            /// <summary> 
            /// Gets the panel. 
            /// </summary> 
            /// <value>The panel.</value> 
            protected virtual HtmlGenericControl Panel 
            { 
                get { return this.Container.GetControl<HtmlGenericControl>("container"true); } 
            } 
     
            /// <summary> 
            /// Gets the calendar. 
            /// </summary> 
            /// <value>The calendar.</value> 
            protected virtual RadCalendar Calendar 
            { 
                get { return this.Container.GetControl<RadCalendar>("calendar"false); } 
            } 
            #endregion 
            #region Overriden methods 
     
            /// <summary> 
            ///  make sure view state is turned on. 
            /// </summary> 
            /// <param name="e"></param> 
            protected override void OnInit(EventArgs e) 
            { 
                base.OnInit(e); 
                if (base.Page != null
                    base.Page.RegisterRequiresControlState(this); 
            } 
     
            /// <summary> 
            /// loads the state 
            /// </summary> 
            /// <param name="savedState"></param> 
            protected override void LoadControlState(object savedState) 
            { 
                if (savedState != null
                { 
                    object[] state = (object[])savedState; 
                    if (state[0] != null
                        this.currentViewDate = (DateTime)state[0]; 
                    this.startDate = (DateTime)state[1]; 
                    this.endDate = (DateTime)state[2]; 
                    if (state[3] != null
                        this.eventsDates = (IList<DateTime>)state[3]; 
                } 
            } 
            /// <summary> 
            /// saves the variable state. 
            /// </summary> 
            /// <returns></returns> 
            protected override object SaveControlState() 
            { 
                return new object[]  
                    { 
                        this.currentViewDate, 
                        this.startDate, 
                        this.endDate, 
                        this.eventsDates 
                    }; 
            } 
     
            /// <summary> 
            /// creates the child controls necessary for this control. 
            /// </summary> 
            protected override void CreateChildControls() 
            { 
                this.Controls.Clear(); 
                this.InitializeCalendar(); 
     
                if (!base.DesignMode) 
                { 
                    this.Controls.Add(this.Container); 
                } 
            } 
            protected override IList CreateDataSource() 
            { 
                return this.Manager.GetContent(0, int.MaxValue, this.SortExpression, ContentStatus.Published, this.GetParentIDs(), this.Filter); 
            } 
            #region Methods 
     
            /// <summary> 
            /// Creates the date range. 
            /// </summary> 
            private void CreateDateRange() 
            { 
     
                int month = this.currentViewDate.Value.Month; 
                int year = this.currentViewDate.Value.Year; 
                int daysInMonth = CultureInfo.InvariantCulture.Calendar.GetDaysInMonth(year, month); 
     
                this.startDate = new DateTime(year, month, 1); 
                this.endDate = new DateTime(year, month, daysInMonth); 
            } 
     
            /// <summary> 
            /// intialtes the calender. 
            /// </summary> 
            private void InitializeCalendar() 
            { 
                if (!base.DesignMode) 
                { 
                    RadCalendar calendar = this.Calendar; 
     
                    if (!this.Page.IsPostBack) 
                    { 
                        if (CurrentViewDate != null
                        { 
                            calendar.SelectedDate = CurrentViewDate.Value.Date; 
                            calendar.FocusedDate = calendar.SelectedDate; 
                            this.currentViewDate = CurrentViewDate.Value; 
                        } 
     
                        if (this.currentViewDate == null
                        { 
                            this.currentViewDate = DateTime.Today; 
                        } 
     
                        BindData(); 
                    } 
     
                    calendar.ShowOtherMonthsDays = false
                    calendar.EnableMultiSelect = false
                    calendar.AutoPostBack = true
                    calendar.DayRender += this.Calendar_DayRender; 
                    calendar.DefaultViewChanged += this.calendar_DefaultViewChanged; 
     
     
                } 
                else 
                { 
                    HtmlGenericControl p = new HtmlGenericControl("p"); 
                    p.Attributes.Add("class""cmsDefaultEmptyControlOv"); 
                    p.InnerHtml = "<strong>Calendar Navigation</strong>"
                    this.Controls.Add(p); 
     
                    HtmlGenericControl p2 = new HtmlGenericControl("p"); 
                    p2.Attributes.Add("class""cmsDefaultEmptyControlCl"); 
                    p2.InnerHtml = "Calendar Navigation"
                    this.Controls.Add(p2); 
                } 
            } 
     
            /// <summary> 
            /// Binds the data. 
            /// </summary> 
            private void BindData() 
            { 
                this.contents = this.CreateDataSource(); 
                this.eventsDates = this.GetEventsDates(); 
            } 
            private IList<DateTime> GetEventsDates() 
            { 
                IList<DateTime> dates = new List<DateTime>(); 
     
                foreach (IContent content in this.contents) 
                { 
                    dates.Add((DateTime)content.GetMetaData("Event_Start")); 
                } 
                return dates; 
            } 
     
            /// <summary> 
            /// Gets the link for date. 
            /// </summary> 
            /// <param name="date">The date.</param> 
            /// <returns></returns> 
            private string GetLinkForDate(DateTime date) 
            { 
                string url = string.Empty; 
                if (this.eventsDates != null
                { 
                    foreach (DateTime postDate in this.eventsDates) 
                    { 
                        if (postDate.Day == date.Day && postDate.Month == date.Month) 
                        { 
                            url = "Year={0}&Month={1}&Day={2}"
                            url = BuildUrl(string.Format(url, date.Year, date.Month, date.Day)); 
                            break
                        } 
                    } 
                } 
                return url; 
            } 
     
            /// <summary> 
            /// builds the url to go to , when a day is clicked. 
            /// </summary> 
            /// <param name="url"></param> 
            /// <returns></returns> 
            private string BuildUrl(string url) 
            { 
                string rawurl; 
     
                rawurl = CmsContext.CurrentUrl != null ? CmsContext.CurrentUrl.Path : this.Context.Request.Url.AbsolutePath; 
     
                if (rawurl.Contains("?")) 
                { 
                    rawurl += "&" + url; 
                } 
                else 
                { 
                    rawurl += "?" + url; 
                } 
     
                // add language extension for multi-lingual setup. 
                IUrlService svc = UrlServices.GetLanguageService(); 
                if (svc != null
                    rawurl = svc.ResolveLanguageUrl(rawurl); 
     
                return rawurl; 
            } 
            #endregion 
            #region Event handlers 
     
            
            void calendar_DefaultViewChanged(object sender, Telerik.Web.UI.Calendar.DefaultViewChangedEventArgs e) 
            { 
                Telerik.Web.UI.Calendar.View.MonthView monthView = e.NewView as Telerik.Web.UI.Calendar.View.MonthView; 
     
                if (monthView != nullthis.currentViewDate = monthView.MonthStartDate; 
     
                BindData(); 
            } 
            void Calendar_DayRender(object sender, Telerik.Web.UI.Calendar.DayRenderEventArgs e) 
            { 
                string url = GetLinkForDate(e.Day.Date); 
     
                e.Cell.Controls.Clear(); 
     
                if (!string.IsNullOrEmpty(url)) 
                { 
                    HtmlGenericControl link = new HtmlGenericControl("a"); 
     
                    link.Attributes.Add("style""text-decoration:underline"); 
     
                    link.Attributes.Add(HtmlTextWriterAttribute.Href.ToString(), url); 
                    link.InnerHtml = e.Day.Date.Day.ToString(); 
     
                    e.Cell.Controls.Add(link); 
                } 
                else 
                { 
                    if (e.Day.Date >= this.startDate && e.Day.Date <= this.endDate) 
                    { 
                        HtmlGenericControl disbledLink = new HtmlGenericControl("a"); 
     
                        disbledLink.Attributes.Add("style""cursor:default;"); 
     
                        disbledLink.Attributes.Add(HtmlTextWriterAttribute.Href.ToString(), "javascript:void(0);"); 
                        disbledLink.InnerHtml = e.Day.Date.Day.ToString(); 
     
                        e.Cell.Controls.Add(disbledLink); 
                    } 
                } 
     
            } 
            #endregion 
            #region Private fields 
     
            private ITemplate layoutTemplate; 
            private string layoutTemplatePath; 
            private GenericContainer container; 
     
            private IList contents; 
            private IList<DateTime> eventsDates; 
            private DateTime? currentViewDate; 
            private DateTime startDate; 
            private DateTime endDate; 
            #endregion 
            #region Constants 
     
            private const string EventsCalendarTemplateName = 
                "Telerik.Events.Resources.ControlTemplates.Frontend.EventsCalendar.ascx"
            #endregion 
        } 


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Rabindra
    Rabindra avatar
    106 posts
    Registered:
    17 May 2009
    16 Aug 2009
    Link to this post
    thanks Ivan for the reply.
    Your sample code was really complicated for me.
    I checked the link you provided and used the code Chanan gave. Everything is fine with it except if I have more than one event on any day then i can only see one event. I think the earliest event.

    what I wanted was if there an event on any date and when I click on it, it should display all the events on that date.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Aug 2009
    Link to this post
    Hi Rabindra,

    GetLinkForDate method from my code sample shows how to add query sting to the url and then get the current view date.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Rabindra
    Rabindra avatar
    106 posts
    Registered:
    17 May 2009
    21 Aug 2009
    Link to this post
    I copied & pasted your above code and I am getting error on "[WebSysTemplate" ...namespace name could not be found. Also all the way at the end bracket I get error.

    Can you please provide me a working code for the following criteria?

    I have a page with Eventsview.
    When page loads I want to show events for today and upto the end of the month. eg. If today is 21st august. I want to show events for today up until 31st aug.

    I will have radcalendar on the right side. if I click any date eg. 25th Aug. I want to show events for that date up until 31 Aug. If I click 2nd Sept. I want to show events for that date up until 30th Sept.

    Also that radcalendar should highlight dates that have events on it.

    Your help is much appreciated.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    21 Aug 2009
    Link to this post
    Hello Rabindra,

    This is a sample code and you should modify some parts of it depending on your requirement. For WebSysTemplate, please take a look at the following post. You can control which events will be shown on EventsView using FilterExpression public property that the control exposes.

    Sample:

    Event_Start >= 2009/08/21 AND Event_Start <= 2009/08/31

    In Calendar_DayRender method you can style the cell as  the sample illustrates where the item is rendered as an underline link.(link.Attributes.Add("style""text-decoration:underline");). ContentBasic class also supports FilterExpression that can be used for the RadCalendar control. Another option for the calendar is overriding Telerik.Cms.Engine.IMetaSearchInfo[] Filter and returning a new filther there.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
6 posts, 0 answered