An Insight into Calendar Recurring Events

With Scheduler .NET you can create events that can be repeated on a daily, weekly, monthly or yearly basis. The recurrence pattern can be changed in accordance with your requirements.

This feature is obtained by updating the logic of the basic scheduler described in the provided documentation and video tutorial.

First of all, you should add new columns to the “Event” table as it is shown in the picture below:

setting recurring events in asp.net calendar

 

‘event_length’ is required to show the length of an event in seconds, while ‘end_date’ determines the moment when the recurring event is over;

‘rec_type’ is the type of event recurring (“day”, “week”, “month” or “year”);

‘event_pid’ allows deleting or changing of a number of events at a row;

Secondly, make updates to ‘BasicSchedulerController.cs’ by adding a new extension that enables recurrence of events:

var scheduler = new DHXScheduler(this);
scheduler.Extensions.Add(SchedulerExtensions.Extension.Recurring);

Afterwards, you should change the action that updates the calendar events. Add extra fields and some logic to the server side controller:

  1. if a changed event has ‘rec_type’=="none", the event status will be “deleted”;
  2. if an event with ‘rec_type’ is either updated or deleted,  all records with ‘event_pid’ are deleted;
  3. if an event with ‘event_pid’ is deleted,  it is updated with ‘rec_type’ = “none”  instead of deleted. 
public ActionResult Save(Recurring changedEvent, FormCollection actionValues)
        {
            var action = new DataAction(actionValues);
 
            DHXSchedulerDataContext data = new DHXSchedulerDataContext();
            try
            {
                bool isFinished = deleteRelated(action, changedEvent, data);
                if (!isFinished)
 
                  
                {
                    switch (action.Type)
                    {
 
                        case DataActionTypes.Insert:
                            data.Recurrings.InsertOnSubmit(changedEvent);
                            if (changedEvent.rec_type == "none")//delete one event from the series
                                action.Type = DataActionTypes.Delete;
                            break;
                        case DataActionTypes.Delete:
                            changedEvent = data.Recurrings.SingleOrDefault(ev => ev.id == action.SourceId);
                            data.Recurrings.DeleteOnSubmit(changedEvent);
                            break;
                        default:// "update"  
                            changedEvent = data.Recurrings.SingleOrDefault(ev => ev.id == action.SourceId);
                            TryUpdateModel(changedEvent);
                            break;
                    }
                }
                data.SubmitChanges();
 
 
                action.TargetId = changedEvent.id;
            }
            catch (Exception a)
            {
                action.Type = DataActionTypes.Error;
            }
 
            return (new AjaxSaveResponse(action));
        }
protected bool deleteRelated(DataAction action, Recurring changedEvent, DHXSchedulerDataContext context)
        {
            bool finished = false;
            if ((action.Type == DataActionTypes.Delete || action.Type == DataActionTypes.Update) && !string.IsNullOrEmpty(changedEvent.rec_type))
            {
                context.Recurrings.DeleteAllOnSubmit(from ev in context.Recurrings where ev.event_pid == changedEvent.id select ev);
            }
 
 
            if (action.Type == DataActionTypes.Delete && changedEvent.id != 0)
            {
                Recurring changed  = (from ev in context.Recurrings where ev.id == action.TargetId select ev).Single() ;
                changed.rec_type = "none";
                finished = true;
            }
            return finished;
        }

 In the end, you get an event calendar with a possibility to enable/disable repeated events: 

recurring events calendar

 

View recurring events demo.

Full code is available in our 30-day trial.

Get DHTMLX Scheduler .NET free trial right now.

free download

If you find this post useful, comment below and share it with your friends.

Author

Svetlana

Viktoria Langer

DHTMLX Scheduler .NET product care manager and customer manager since 2012. Interested in ASP.NET and trying to create valuable content.

Recent Blog Posts