Calendar and SQL Server Tutorial (C# and VB.NET)

mvc calendar

Note: This tutorial shows how to use DayPilot Pro ASP.NET MVC event calendar with ASP.NET MVC 2. See also the updated versions of this tutorial:

Download

Prerequisites

Installation

DLL

  • Put DayPilot.Mvc.dll in the bin directory of the web application.
  • Add it to project references if you are using a compiled web project.

Scripts

Include the following scripts in the web page (the scripts can be found in Scripts/DayPilot directory):

  • common.js
  • calendar.js

All the scripts are included in the site template (Views/Shared/Site.master):

<head runat="server">
    <!-- ... -->
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/DayPilot/common.js") %>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/DayPilot/calendar.js") %>"></script>
</head>

CSS Themes

Include the Themes directory from the DayPilot demo project.

Include Themes/themes.css stylesheet in the web page:

<head runat="server">
    <!-- ... -->
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/Themes/themes.css") %>" /> 
</head>

View

Add the Calendar control to the view (Views/Home/Index.aspx):

C#

<%=Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig {})%>

VB.NET

<%= Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig)%>

This will add an empty Calendar with default settings.

In order to show calendar data, we need to specify the backend url:

C#

    <p>
    <%=Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig
       {
           BackendUrl = ResolveUrl("~/Calendar/Backend")
       })%>
    </p>

VB.NET

    <p>
        <%= Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig With {
            .BackendUrl = ResolveUrl("~/Calendar/Backend")
        })%>
    </p>

Backend Controller

Create a new CalendarController classthat will handle the backend requests. Add a Backend method:

C#

    public class CalendarController : Controller
    {
        public ActionResult Backend()
        {
            return null;
        }
    }

VB.NET

    Public Class CalendarController
        Inherits System.Web.Mvc.Controller

        Function Backend() As ActionResult
            Return Nothing
        End Function

    End Class

Create a  new Dpc class that will derive from DayPilot.Web.Mvc.DayPilotCalendar:

C#

public class Dpc : DayPilotCalendar
{
}

VB.NET

Public Class Dpc
    Inherits DayPilotCalendar

End Class

Modify the Backend action of the Calendar controller to pass control to a new instance of the Dpc class:

C#

public class CalendarController : Controller
{
    public ActionResult Backend()
    {
        return new Dpc().CallBack(this);
    }
}

VB.NET

    Public Class CalendarController
        Inherits System.Web.Mvc.Controller

        Function Backend() As ActionResult
            Return New Dpc().CallBack(Me)
        End Function

    End Class

Now override OnInit() method of the DayPilotCalendar class in the Dpc class in order to handle the Init event:

C#

public class Dpc : DayPilotCalendar
{
     protected override void OnInit(InitArgs initArgs)
     {
         Events = new EventManager().FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable();

DataStartField = "eventstart";
DataEndField = "eventend";
DataTextField = "name";
DataIdField = "id";
                         UpdateWithMessage("Welcome!"); } }

VB.NET

        Public Class Dpc
            Inherits DayPilotCalendar

            Protected Overrides Sub OnInit(ByVal e As DayPilot.Web.Mvc.Events.Calendar.InitArgs)
                Dim em = New EventManager()
                Events = em.FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable

                DataStartField = "eventstart"
                DataEndField = "eventend"
                DataTextField = "name"
                DataIdField = "id"

                UpdateWithMessage("Welcome!")
            End Sub
        End Class

This OnInit() method does three things:

  1. It loads the event set to Events property.
  2. It maps the event set fields (id, start, end, text) to DayPilot fields.
  3. It tells the Calendar to refresh the events and show a welcome message on the client side.

EventManager is a helper class that loads data from a database:

C#

    public class EventManager
    {
        public DataTable FilteredData(DateTime start, DateTime end)
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [event] WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))", ConfigurationManager.ConnectionStrings["daypilot"].ConnectionString);
            da.SelectCommand.Parameters.AddWithValue("start", start);
            da.SelectCommand.Parameters.AddWithValue("end", end);

            DataTable dt = new DataTable();
            da.Fill(dt);

            return dt;
        }
    }

VB.NET

Public Class EventManager
    Public Function FilteredData(ByVal start As DateTime, ByVal end_ As DateTime) As DataTable
        Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [event] WHERE NOT (([eventend] <= @start) OR ([eventstart] >= @end))", ConfigurationManager.ConnectionStrings("daypilot").ConnectionString)
        da.SelectCommand.Parameters.AddWithValue("start", start)
        da.SelectCommand.Parameters.AddWithValue("end", end_)

        Dim dt = New DataTable
        da.Fill(dt)
        Return dt

    End Function
End Class

Adding a New Event

In order to handle time range selection event (selecting time cells using a mouse drag), add TimeRangeSelectedHandling property to the view:

C#

    <%=Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig
       {
          BackendUrl = ResolveUrl("~/Calendar/Backend"),
          TimeRangeSelectedHandling = TimeRangeSelectedHandlingType.CallBack
       })%>

VB.NET

        <%= Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig With {
            .BackendUrl = ResolveUrl("~/Calendar/Backend"),
            .TimeRangeSelectedHandling = DayPilot.Web.Mvc.Events.Calendar.TimeRangeSelectedHandlingType.CallBack
        })%>

And override OnTimeRangeSelected method in the Dpc class:

C#

            protected override void OnTimeRangeSelected(TimeRangeSelectedArgs e)
            {
                new EventManager().EventCreate(e.Start, e.End, "New event");

                Events = new EventManager().FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable();

                DataStartField = "eventstart";
                DataEndField = "eventend";
                DataTextField = "name";
                DataIdField = "id";

                UpdateWithMessage("A new event was created");
            }

VB.NET

            Protected Overrides Sub OnTimeRangeSelected(ByVal e As TimeRangeSelectedArgs)
                Dim em = New EventManager()
                em.EventCreate(e.Start, e.End, "New event")

                Events = em.FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable                 DataStartField = "eventstart"                 DataEndField = "eventend"                 DataTextField = "name"                 DataIdField = "id"
                UpdateWithMessage("A new event was created")
            End Sub

The OnTimeRangeSelected() method does three things:

  1. It creates a new record in the database.
  2. It loads the event set to Events property.
  3. It maps the event set fields (id, start, end, text) to DayPilot fields.
  4. It tells the Calendar to refresh the events and show a welcome message on the client side.

You can see that the steps 2-4 are the same as in OnInit(). We can move the events reloading to an OnFinish() method which is called during every CallBack after the main event handler.

C#

protected override void OnInit(InitArgs initArgs)
{
  Events = new EventManager().FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable();
  UpdateWithMessage("Welcome!");
}

protected override void OnTimeRangeSelected(TimeRangeSelectedArgs e)
{
  new EventManager().EventCreate(e.Start, e.End, "New event");
  UpdateWithMessage("A new event was created");
}

protected override void OnFinish()
{
  // update was not requested in the event handler using Update() or UpdateWithMessage()
  // skip event reloading
  if (UpdateType == CallBackUpdateType.None)
  {
    return;
  }

  Events = new EventManager().FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable();

  DataStartField = "eventstart";
  DataEndField = "eventend";
  DataTextField = "name";
  DataIdField = "id";
}

VB.NET

Protected Overrides Sub OnInit(ByVal e As DayPilot.Web.Mvc.Events.Calendar.InitArgs)
  UpdateWithMessage("Welcome!")
End Sub

Protected Overrides Sub OnTimeRangeSelected(ByVal e As TimeRangeSelectedArgs)
  Dim em = New EventManager()
  em.EventCreate(e.Start, e.End, "New event")
  UpdateWithMessage("A new event was created")
End Sub

Protected Overrides Sub OnFinish()
  REM update was not requested in the event handler using Update() or UpdateWithMessage()
  REM skip event reloading
  If (UpdateType = Enums.CallBackUpdateType.None) Then
    Return
  End If

  Dim em = New EventManager()
  Events = em.FilteredData(StartDate, StartDate.AddDays(Days)).AsEnumerable

  DataStartField = "eventstart"
  DataEndField = "eventend"
  DataTextField = "name"
  DataIdField = "id"
End Sub

Event Moving

Similar steps have to be applied in order to enable event moving using drag&drop.

Add EventMoveHandling property to the view:

C#

    <%=Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig
       {
          BackendUrl = ResolveUrl("~/Calendar/Backend"),
          TimeRangeSelectedHandling = TimeRangeSelectedHandlingType.CallBack,
          EventMoveHandling = EventMoveHandlingType.CallBack
       })%>

VB.NET

        <%= Html.DayPilotCalendar("dpc", new DayPilotCalendarConfig With {
            .BackendUrl = ResolveUrl("~/Calendar/Backend"),
            .TimeRangeSelectedHandling = DayPilot.Web.Mvc.Events.Calendar.TimeRangeSelectedHandlingType.CallBack,
            .EventMoveHandling = DayPilot.Web.Mvc.Events.Calendar.EventMoveHandlingType.CallBack
        })%>

Override OnEventMove() method in the Dpc class:

C#

protected override void OnEventMove(EventMoveArgs e)
{
  new EventManager().EventMove(e.Id, e.NewStart, e.NewEnd);
  UpdateWithMessage("The event was moved");
}

VB.NET

Protected Overrides Sub OnEventMove(ByVal e As DayPilot.Web.Mvc.Events.Calendar.EventMoveArgs)
  Dim em = New EventManager()
  em.EventMove(e.Id, e.NewStart, e.NewEnd)
  UpdateWithMessage("The event was moved") End Sub