New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Limit the number of concurrent appointments just for a single resource.
HOW-TO
Limit the number of concurrent appointments just for a single resource.
DESCRIPTION
This is an extension to the example on Limiting the number of concurrent appointments. With the added functionality to the attached sample, the limitation for concurrent appointments is done just for a single resource. For example, if there is an appointment scheduled for Group A, you can schedule another appointment in the same time for Group B.
SOLUTION
C#
private const int AppointmentsLimit = 1;
private void Page_Load(object sender, EventArgs e)
{
}
protected bool ExceedsLimitForResource(Appointment apt, string resourceType)
{
int appointmentsCount = 0;
if (apt.Resources.GetResourceByType(resourceType) != null)
{
foreach (Appointment a in RadScheduler1.Appointments.GetAppointmentsInRange(apt.Start, apt.End))
{
if (apt.Resources.GetResourceByType(resourceType).Key.ToString() == a.Resources.GetResourceByType(resourceType).Key.ToString())
{
//If inserting a new appontment apt.ID will be null so we need to handle this case:
if (apt.ID != null)
{
//This check is needed to allow an update for an existing appointment through the
//edit form or advanced edit form. If we don't handle this case an update of an appointment
//is only possible through drag-and-drop.
if (apt.ID.ToString() != a.ID.ToString())
appointmentsCount++;
}
else
{
appointmentsCount++;
}
}
}
}
else
{
appointmentsCount = RadScheduler1.Appointments.GetAppointmentsInRange(apt.Start, apt.End).Count;
}
return (appointmentsCount > AppointmentsLimit - 1);
}
protected void RadScheduler1_AppointmentInsert(object sender, Telerik.Web.UI.SchedulerCancelEventArgs e)
{
if (ExceedsLimitForResource(e.Appointment, RadScheduler1.GroupBy))
{
Label1.Text = "Another appointment exists in this time slot.";
e.Cancel = true;
}
}
protected void RadScheduler1_AppointmentUpdate(object sender, Telerik.Web.UI.AppointmentUpdateEventArgs e)
{
if (ExceedsLimitForResource(e.ModifiedAppointment, RadScheduler1.GroupBy))
{
foreach (Appointment a in RadScheduler1.Appointments.GetAppointmentsInRange(e.ModifiedAppointment.Start, e.ModifiedAppointment.End))
{
if (a.ID != e.Appointment.ID)
{
Label1.Text = "Another appointment exists in this time slot.";
e.Cancel = true;
}
}
}
}
protected void RadScheduler1_AppointmentCancelingEdit(object sender, AppointmentCancelingEditEventArgs e)
{
Label1.Text = "";
}