Example E4015
Visible to All Users

Scheduler for ASP.NET MVC - How to customize the Go to Date dialog

This example demonstrates how to implement a custom Go to Date dialog.

Implementation Details

  1. Call the SetGotoDateFormTemplateContent method to specify custom content for the Go to Date form. Add a single Calendar extension to the form.
    C#
    settings.OptionsForms.SetGotoDateFormTemplateContent(c => { Html.DevExpress().Calendar(calendarSettings => { calendarSettings.Name = "calendar"; calendarSettings.Properties.ClientSideEvents.SelectionChanged = "OnCalendarSelectionChanged"; calendarSettings.Properties.ShowClearButton = false; }).Bind(c.Date).Render(); });
  2. Specify the CalendarClientSideEvents.SelectionChanged property to subscribe to selection changes in the calendar. The event handler calls the ASPxClientScheduler.GoToDateFormApply method to initiate a callback to the server.
    Code
    function OnCalendarSelectionChanged(s, e){ gotoDateCallback = true; scheduler.GoToDateFormApply(); }
  3. Handle the MVCxSchedulerClientSideEvents.BeginCallback event to obtain a new date from the calendar and pass it to the server.
    Code
    function OnSchedulerBeginCallback(s, e){ if(gotoDateCallback){ e.customArgs["NewDate"] = calendar.GetValueString(); gotoDateCallback = false; } }
  4. Implement a special callback command to handle this request properly. Create a DevExpress.Web.ASPxScheduler.Internal.GotoDateFormCallbackCommand class successor and override its ParseParameters and ExecuteCore methods. In the ExecuteCore method, call methods of the IDateTimeNavigationService interface to perform date navigation in the scheduler.
    C#
    public class MVCxSchedulerGotoDateFormCallbackCommand: GotoDateFormCallbackCommand { public MVCxSchedulerGotoDateFormCallbackCommand(MVCxScheduler control) : base(control) { } public new DateTime NewDate { get; protected set; } protected override void ParseParameters(string parameters) { string rawDate = HttpContext.Current.Request["NewDate"]; NewDate = DateTime.Parse(rawDate); } protected override void ExecuteCore() { IDateTimeNavigationService service = (IDateTimeNavigationService)Control.GetService(typeof(IDateTimeNavigationService)); if (service != null) service.GoToDate(NewDate); } }
  5. Create a delegate method for the SchedulerSettings.BeforeExecuteCallback property to execute your custom command instead of the default command when the GotoDateForm command is queried for execution.
    C#
    settings.BeforeExecuteCallbackCommand = (sender, e) => { if(e.CommandId == SchedulerCallbackCommandId.GotoDateForm) e.Command = new MVCxSchedulerGotoDateFormCallbackCommand((MVCxScheduler)sender); };

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Scheduler.CustomizationGotoDateForm/Models/SchedulingDataClasses.cs
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Collections; using DevExpress.Web.Mvc; using System.Configuration; namespace Scheduler.CustomizationGotoDateForm.Models { public static class SchedulingDBDataProvider { const string SchedulingDBDataContextKey = "DXNorthwindDataContext"; public static SchedulingDataClassesDataContext DB { get { if (HttpContext.Current.Items[SchedulingDBDataContextKey] == null) { SchedulingDataClassesDataContext context = new SchedulingDataClassesDataContext(); HttpContext.Current.Items[SchedulingDBDataContextKey] = new SchedulingDataClassesDataContext(); } return (SchedulingDataClassesDataContext)HttpContext.Current.Items[SchedulingDBDataContextKey]; } } public static IEnumerable GetResources() { return from res in DB.DBResources where res.ResourceID < 6 select new { ID = res.ResourceID, Model = res.ResourceName }; } public static IList<Schedule> GetAppointments() { string key = "Apts"; IList<Schedule> carScheduling = (IList<Schedule>)HttpContext.Current.Session[key]; if (carScheduling == null) { carScheduling = (from schedule in DB.DBAppointments select new Schedule() { ID = schedule.UniqueID, Subject = schedule.Subject, Description = schedule.Description, StartTime = schedule.StartDate, EndTime = schedule.EndDate, EventType = schedule.Type, Label = schedule.Label, AllDay = (bool)schedule.AllDay, Location = schedule.Location, ResourceID = schedule.ResourceID.ToString(), Status = schedule.Status, RecurrenceInfo = schedule.RecurrenceInfo }).ToList(); HttpContext.Current.Session[key] = carScheduling; } return carScheduling; } public static void InsertSchedule(Schedule schedule) { if (schedule == null) return; Schedule editableSchedule = new Schedule(); editableSchedule.Assign(schedule); editableSchedule.ID = GenerateScheduleID(); GetAppointments().Add(editableSchedule); } public static void UpdateSchedule(Schedule schedule) { if (schedule == null) return; Schedule editableSchedule = GetScheduleByID(schedule.ID); editableSchedule.Assign(schedule); } public static void DeleteCarScheduling(Schedule schedule) { if (schedule == null) return; Schedule editableSchedule = GetScheduleByID(schedule.ID); if (editableSchedule != null) GetAppointments().Remove(editableSchedule); } public static Schedule GetScheduleByID(int id) { return (from schedule in GetAppointments() where schedule.ID == id select schedule).FirstOrDefault(); } static int GenerateScheduleID() { IList<Schedule> carScheduling = GetAppointments(); return (carScheduling.Count() > 0) ? carScheduling.Last().ID + 1 : 0; } } public class Schedule { public int ID { get; set; } public string Subject { get; set; } public string Description { get; set; } public DateTime? StartTime { get; set; } public DateTime? EndTime { get; set; } public int? EventType { get; set; } public int? Label { get; set; } public bool AllDay { get; set; } public string Location { get; set; } public string ResourceID { get; set; } public int? Status { get; set; } public string RecurrenceInfo { get; set; } public virtual void Assign(Schedule schedule) { if (schedule == null) return; ID = schedule.ID; Subject = schedule.Subject; Description = schedule.Description; StartTime = schedule.StartTime; EndTime = schedule.EndTime; EventType = schedule.EventType; Label = schedule.Label; AllDay = schedule.AllDay; Location = schedule.Location; ResourceID = schedule.ResourceID; Status = schedule.Status; RecurrenceInfo = schedule.RecurrenceInfo; } } public class SchedulerDataObject { public IEnumerable Appointments { get; set; } public IEnumerable Resources { get; set; } } public class SchedulerHelper { static MVCxAppointmentStorage appointmentStorage; public static MVCxAppointmentStorage AppointmentStorage { get { if (appointmentStorage == null) appointmentStorage = CreateAppointmentStorage(); return appointmentStorage; } } static MVCxAppointmentStorage CreateAppointmentStorage() { MVCxAppointmentStorage appointmentStorage = new MVCxAppointmentStorage(); appointmentStorage.Mappings.AppointmentId = "ID"; appointmentStorage.Mappings.Start = "StartTime"; appointmentStorage.Mappings.End = "EndTime"; appointmentStorage.Mappings.Subject = "Subject"; appointmentStorage.Mappings.Description = "Description"; appointmentStorage.Mappings.Location = "Location"; appointmentStorage.Mappings.AllDay = "AllDay"; appointmentStorage.Mappings.Type = "EventType"; appointmentStorage.Mappings.RecurrenceInfo = "RecurrenceInfo"; appointmentStorage.Mappings.ReminderInfo = "ReminderInfo"; appointmentStorage.Mappings.Label = "Label"; appointmentStorage.Mappings.Status = "Status"; appointmentStorage.Mappings.ResourceId = "CarId"; return appointmentStorage; } static MVCxResourceStorage resourceStorage; public static MVCxResourceStorage ResourceStorage { get { if (resourceStorage == null) resourceStorage = CreateResourceStorage(); return resourceStorage; } } static MVCxResourceStorage CreateResourceStorage() { MVCxResourceStorage resourceStorage = new MVCxResourceStorage(); resourceStorage.Mappings.ResourceId = "ID"; resourceStorage.Mappings.Caption = "Model"; return resourceStorage; } public static SchedulerDataObject DataObject { get { return new SchedulerDataObject() { Appointments = SchedulingDBDataProvider.GetAppointments(), Resources = SchedulingDBDataProvider.GetResources() }; } } } }
Scheduler.CustomizationGotoDateForm/Views/Scheduler/Scheduler.cshtml
Razor
@model SchedulerDataObject @{ ViewBag.Title = "Scheduler"; } <script type="text/javascript"> var gotoDateCallback = false; function OnCalendarSelectionChanged(s, e){ gotoDateCallback = true; scheduler.GoToDateFormApply(); } function OnSchedulerBeginCallback(s, e){ if(gotoDateCallback){ e.customArgs["NewDate"] = calendar.GetValueString(); gotoDateCallback = false; } } </script> <h2>Scheduler</h2> @Html.Partial("SchedulerPartial", Model)
Scheduler.CustomizationGotoDateForm/Views/Scheduler/SchedulerPartial.cshtml
Razor
@using Scheduler.CustomizationGotoDateForm.ExtensionHelpers @Html.DevExpress().Scheduler( settings => { settings.Name = "scheduler"; settings.CallbackRouteValues = new { Controller = "Scheduler", Action = "SchedulerPartial" }; settings.EditAppointmentRouteValues = new { Controller = "Scheduler", Action = "EditAppointmentAction" }; settings.Width = 800; settings.Start = new DateTime(2012, 05, 14); settings.Views.DayView.Styles.ScrollAreaHeight = 300; settings.ClientSideEvents.BeginCallback = "OnSchedulerBeginCallback"; settings.BeforeExecuteCallbackCommand = (sender, e) => { if(e.CommandId == SchedulerCallbackCommandId.GotoDateForm) e.Command = new MVCxSchedulerGotoDateFormCallbackCommand((MVCxScheduler)sender); }; settings.OptionsForms.SetGotoDateFormTemplateContent(c => { Html.DevExpress().Calendar(calendarSettings => { calendarSettings.Name = "calendar"; calendarSettings.Properties.ClientSideEvents.SelectionChanged = "OnCalendarSelectionChanged"; calendarSettings.Properties.ShowClearButton = false; }).Bind(c.Date).Render(); }); settings.Storage.Appointments.Assign(SchedulerHelper.AppointmentStorage); settings.Storage.Resources.Assign(SchedulerHelper.ResourceStorage); }).Bind(Model.Appointments, Model.Resources).GetHtml()

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.