This is a migrated thread and some comments may be shown as answers.

how to bind a RadScheduler to a object List of type Telerick.Web.Ui.Appointment

1 Answer 161 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Allen
Top achievements
Rank 1
Allen asked on 19 Jul 2012, 01:43 PM
Can anyone give me a sample on how to bind a RadScheduler to a object List of type Telerick.Web.Ui.Appointment?
Here is my VB:

Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Configuration
Imports System.Web.Security
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Telerik.Web.UI
Imports System.Drawing

Public Class MyAvailability
    Inherits System.Web.UI.Page
    Private Class CustomAppointmentComparer
        Implements IComparer(Of Appointment)
        Public Function Compare(first As Appointment, second As Appointment) As Integer Implements IComparer(Of Telerik.Web.UI.Appointment).Compare
            Dim infoStart1 As String = first.Start.ToString
            Dim infoend1 As String = first.End.ToString
            Dim infoStar2 As String = second.Start.ToString
            Dim infoend2 As String = second.End.ToString
            If first Is Nothing OrElse second Is Nothing Then
                Throw New InvalidOperationException("Can't compare null object(s).")
            End If

            If [String].Compare(first.Subject.ToString, second.Subject.ToString) < 0 Then
                Return -1
            End If

            If [String].Compare(first.Subject.ToString, second.Subject.ToString) > 0 Then
                Return 1
            End If

            Return 0
        End Function
    End Class

    Protected Overrides Sub OnInit(e As EventArgs)
        MyBase.OnInit(e)
        RadScheduler1.Provider = New XmlSchedulerProvider(Server.MapPath("~/App_Data/Appointments.xml"), True)
    End Sub

    Protected Sub RadScheduler1_AppointmentDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.SchedulerEventArgs) Handles RadScheduler1.AppointmentDataBound
        e.Appointment.CssClass = "MyCustomAppointmentStyle"
        If e.Appointment.Subject = "Availability" Then
            e.Appointment.CssClass = "•rsCategoryYellow"
        Else
            e.Appointment.CssClass = "rsCategoryGreen"
        End If
        ''e.Appointment.
    End Sub

    Protected Sub RadScheduler1_AppointmentCreated(sender As Object, e As AppointmentCreatedEventArgs) Handles RadScheduler1.AppointmentCreated
        ''If e.Appointment.Subject = "Prefered Availability" Then
        If e.Appointment.Subject = "Preference" Then

            e.Appointment.BackColor = System.Drawing.Color.Green
        End If
        If e.Appointment.Subject = "Availability" Then

            e.Appointment.BackColor = System.Drawing.Color.FromName("#FEEDA9")
        End If
        If e.Appointment.Visible AndAlso Not IsAppointmentRegisteredForTooltip(e.Appointment) Then
            Dim id As String = e.Appointment.ID.ToString()

            For Each domElementID As String In e.Appointment.DomElements
                RadToolTipManager1.TargetControls.Add(domElementID, id, True)
            Next
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        RadToolTipManager1.OnClientRequestStart = "OnClientRequestStart"
        RadScheduler1.AppointmentComparer = New CustomAppointmentComparer()
        Dim appointments As Appointment() = RadScheduler1.Appointments.ToArray()
        Array.Sort(appointments, New CustomAppointmentComparer())
        For Each appointment As Appointment In appointments
            Dim appointmentStart As DateTime = RadScheduler1.UtcToDisplay(appointment.Start)
            If appointmentStart > RadScheduler1.SelectedDate Then
                RadScheduler1.SelectedDate = appointmentStart.[Date]
                Exit For
            End If
        Next
    End Sub

    Protected Sub RadScheduler1_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles RadScheduler1.DataBound
        RadToolTipManager1.TargetControls.Clear()
        ScriptManager.RegisterStartupScript(Me, GetType(System.Web.UI.Page), "HideToolTip", "hideActiveToolTip();", True)
        Dim monthNum As Integer = RadScheduler1.VisibleRangeStart.Month
        Dim strDate As New DateTime(1, monthNum, 1)           
        RadScheduler1.Localization.AllDay = strDate.ToString("MMMMM")
    End Sub

    Protected Sub MyAvailability1_FormCreated(ByVal sender As Object, ByVal e As SchedulerFormCreatedEventArgs) Handles RadScheduler1.FormCreated
        If (e.Container.Mode = SchedulerFormMode.AdvancedEdit) OrElse (e.Container.Mode = SchedulerFormMode.AdvancedInsert) Then
            Dim allDayCheckbox As CheckBox = DirectCast(e.Container.FindControl("AllDayEvent"), CheckBox)
            allDayCheckbox.Style.Add("visibility", "hidden")
            Dim attrAnnotationsTextbox As RadTextBox = DirectCast(e.Container.FindControl("Subject"), RadTextBox)
            attrAnnotationsTextbox.Label = "Type"
            attrAnnotationsTextbox.Enabled = False
            Dim AppointmentID As Integer = e.Appointment.ID
            Dim AppointmentIDPrevious As Integer = e.Appointment.ID - 1
            Dim PreviousAppointment As New Appointment

            ''Dim startPicker As RadTimePicker = DirectCast(e.Container.FindControl("StartTime"), RadTimePicker)
            ''startPicker.TimeView.StartTime = TimeSpan.FromHours(1.5)
            ''startPicker.TimeView.EndTime = TimeSpan.FromHours(24)
            ''startPicker.TimeView.DataList.DataSource = Nothing
            ''startPicker.TimeView.DataBind()

            ''Dim endPicker As RadTimePicker = DirectCast(e.Container.FindControl("EndTime"), RadTimePicker)
            ''endPicker.TimeView.StartTime = TimeSpan.FromHours(8)
            ''endPicker.TimeView.EndTime = TimeSpan.FromHours(24)
            ''endPicker.TimeView.DataList.DataSource = Nothing
            ''endPicker.TimeView.DataBind()

            '' Dim StartTimeAppointmentIDPrevious As String = RadScheduler1.Appointments.FindByID(52).Start.ToString
            '' Catch ex As Exception
            ''ex.ToString()
            '' End Try
            Dim scheduler As RadScheduler = DirectCast(sender, RadScheduler)
            Dim StartTimeAvail As DateTime
            Dim EndTimeAvail As DateTime
            If e.Appointment.Subject = "Preference" Then

                For Each app As Appointment In scheduler.Appointments.GetAppointmentsInRange(e.Appointment.Start, e.Appointment.[End])

                    If app.Subject = "Availability" Then
                        StartTimeAvail = app.Start.ToShortTimeString
                        EndTimeAvail = app.End.ToShortTimeString
                        Exit For
                    End If

                Next

            End If
            Dim startPicker As RadTimePicker = DirectCast(e.Container.FindControl("StartTime"), RadTimePicker)
            Dim endPicker As RadTimePicker = DirectCast(e.Container.FindControl("EndTime"), RadTimePicker)
            Dim StartDbleTime As Double = StartTimeAvail.ToOADate() * 24
            Dim EndDbleTime As Double = EndTimeAvail.ToOADate() * 24 + 0.5
            startPicker.TimeView.StartTime = TimeSpan.FromHours(StartDbleTime)
            ''startPicker.TimeView.EndTime = TimeSpan.FromHours(24)
            startPicker.TimeView.EndTime = TimeSpan.FromHours(EndDbleTime)
            endPicker.TimeView.StartTime = TimeSpan.FromHours(StartDbleTime)
            endPicker.TimeView.EndTime = TimeSpan.FromHours(EndDbleTime)

            startPicker.TimeView.DataList.DataSource = Nothing
            startPicker.TimeView.DataBind()
            endPicker.TimeView.DataList.DataSource = Nothing
            endPicker.TimeView.DataBind()
            ''Dim startTime As RadTimePicker = TryCast(e.Container.FindControl("StartTime"), RadTimePicker)
            ''Dim DbleTime As Double = ADate.ToOADate() * 24
            ''startPicker.TimeView.StartTime = TimeSpan.FromHours(DbleTime)
        End If
    End Sub

    Protected Sub MyAvailability1_AppointmentInsert(sender As Object, e As AppointmentInsertEventArgs) Handles RadScheduler1.AppointmentInsert
        If e.Appointment.Subject = "Preference" Then
            Dim scheduler As RadScheduler = DirectCast(sender, RadScheduler)
            For Each app As Appointment In scheduler.Appointments.GetAppointmentsInRange(e.Appointment.Start, e.Appointment.[End])
                If app.Subject = "Availability" Then
                    If app.Start > e.Appointment.Start OrElse app.[End] <= e.Appointment.[End] Then
                        e.Cancel = True
                    End If
                End If

            Next
        End If

    End Sub
    Protected Sub MyAvailability1_AppointmentUpdate(sender As Object, e As AppointmentUpdateEventArgs) Handles RadScheduler1.AppointmentUpdate
        Dim scheduler As RadScheduler = DirectCast(sender, RadScheduler)
        If e.ModifiedAppointment.Subject = "Preference" Then
            For Each app As Appointment In scheduler.Appointments.GetAppointmentsInRange(e.ModifiedAppointment.Start, e.ModifiedAppointment.[End])

                If app.Subject = "Availability" Then
                    If app.Start > e.ModifiedAppointment.Start OrElse app.[End] < e.ModifiedAppointment.[End] Then
                        e.Cancel = True
                    End If
                End If

            Next
        End If

        If e.ModifiedAppointment.Subject = "Availability" Then
            For Each app As Appointment In scheduler.Appointments.GetAppointmentsInRange(e.ModifiedAppointment.Start, e.ModifiedAppointment.[End])

                If app.Subject = "Preference" Then
                    If app.Start < e.ModifiedAppointment.Start OrElse app.[End] > e.ModifiedAppointment.[End] Then
                        e.Cancel = True
                    End If
                End If
            Next
        End If

    End Sub

    Private Function IsAppointmentRegisteredForTooltip(ByVal apt As Appointment) As Boolean
        For Each targetControl As ToolTipTargetControl In RadToolTipManager1.TargetControls
            If (apt.DomElements.Contains(targetControl.TargetControlID)) Then
                Return True
            End If
        Next
        Return False
    End Function

    Protected Sub RadToolTipManager1_AjaxUpdate(ByVal sender As Object, ByVal e As ToolTipUpdateEventArgs)
        Dim aptId As Integer
        Dim apt As Appointment
        If Not Integer.TryParse(e.Value, aptId) Then
            'The appoitnment is occurrence and FindByID expects a string
            apt = RadScheduler1.Appointments.FindByID(e.Value)
        Else
            'The appointment is not occurrence and FindByID expects an int
            apt = RadScheduler1.Appointments.FindByID(aptId)
        End If
        ''Dim toolTip As AppointmentToolTip = CType(LoadControl("AppointmentToolTip.ascx"), AppointmentToolTip)
        ''toolTip.TargetAppointment = apt
        ''e.UpdatePanel.ContentTemplateContainer.Controls.Add(toolTip)
    End Sub

    Public newStartLabel As String = ""
    Public newEndLabel As String = ""
    Public dayOfWeek As String = ""
    Protected Sub RadScheduler1_AppointmentUpdate(sender As Object, e As AppointmentUpdateEventArgs)
        If e.ModifiedAppointment.Subject = "Availability" Then
            newStartLabel = e.ModifiedAppointment.Start.ToString()
            newEndLabel = e.ModifiedAppointment.[End].ToString()
            dayOfWeek = e.ModifiedAppointment.Start.DayOfWeek.ToString()
        End If
    End Sub

    Protected Sub RadScheduler1_TimeSlotCreated(sender As Object, e As TimeSlotCreatedEventArgs) Handles RadScheduler1.TimeSlotCreated
        Dim scheduler As RadScheduler = DirectCast(sender, RadScheduler)
        ''If scheduler.SelectedView = SchedulerViewType.WeekView AndAlso e.TimeSlot.Duration = TimeSpan.FromDays(1) Then

        If e.TimeSlot.Duration = TimeSpan.FromDays(1) Then
            Dim startLabel As New Label()
            Dim startLabelPref As New Label()
            startLabel.ID = "startLabel" + e.TimeSlot.Start.DayOfWeek.ToString + CType(e.TimeSlot.Start.Day, String)
            startLabelPref.ID = "startLabelPref" + e.TimeSlot.Start.DayOfWeek.ToString + CType(e.TimeSlot.Start.Day, String)
            startLabel.Font.Size = 7
            startLabel.Font.Bold = True
            startLabelPref.Font.Size = 7
            startLabelPref.Font.Bold = True

            If newStartLabel <> "" AndAlso dayOfWeek = e.TimeSlot.Start.DayOfWeek.ToString() Then
                startLabel.Text = newStartLabel
            Else
                For Each app As Appointment In RadScheduler1.Appointments.GetAppointmentsInRange(e.TimeSlot.Start, e.TimeSlot.[End])
                    If app.Subject = "Availability" Then
                        Dim TimeFormat As String
                        TimeFormat = Trim(app.Start.ToShortTimeString)
                        ''TimeFormat.Replace(" AM", "a")
                        If TimeFormat.Contains("AM") Then
                            startLabel.Text = "   " + TimeFormat.Replace(" AM", "a")
                        Else
                            startLabel.Text = "   " + app.Start.ToShortTimeString.Replace(" PM", "p")
                        End If
                        startLabel.BackColor = System.Drawing.Color.FromName("#FEEDA9")

                    End If

                    If app.Subject = "Preference" Then
                        Dim TimeFormat As String
                        TimeFormat = Trim(app.Start.ToShortTimeString)
                        ''TimeFormat.Replace(" AM", "a")
                        If TimeFormat.Contains("AM") Then
                            startLabelPref.Text = TimeFormat.Replace(" AM", "a")
                        Else
                            startLabelPref.Text = app.Start.ToShortTimeString.Replace(" PM", "p")
                        End If

                        startLabelPref.BackColor = System.Drawing.Color.FromName("#D0ECBB")
                    End If
                Next
            End If
            Dim endLabel As New Label()
            Dim endLabelPref As New Label()
            ''startLabel.ID = "endLabel" + e.TimeSlot.Start.DayOfWeek.ToString
            endLabel.ID = "endLabel" + e.TimeSlot.End.DayOfWeek.ToString + CType(e.TimeSlot.End.Day, String)
            ''startLabelPref.ID = "endLabelPref" + e.TimeSlot.Start.DayOfWeek.ToString
            endLabelPref.ID = "endLabelPref" + e.TimeSlot.End.DayOfWeek.ToString + CType(e.TimeSlot.End.Day, String)
            endLabel.Font.Size = 7
            endLabel.Font.Bold = True
            endLabelPref.Font.Size = 7
            endLabelPref.Font.Bold = True
            ''If newStartLabel <> "" AndAlso dayOfWeek = e.TimeSlot.Start.DayOfWeek.ToString() Then
            If newEndLabel <> "" AndAlso dayOfWeek = e.TimeSlot.End.DayOfWeek.ToString() Then
                endLabel.Text = "-" + newEndLabel
            Else
                For Each app As Appointment In RadScheduler1.Appointments.GetAppointmentsInRange(e.TimeSlot.Start, e.TimeSlot.[End])
                    If app.Subject = "Availability" Then
                        Dim TimeFormat2 As String
                        TimeFormat2 = Trim("-" + app.[End].ToShortTimeString)
                        endLabel.Text = TimeFormat2.Replace(" AM", "a")
                        endLabel.Text = "-" + app.[End].ToShortTimeString.Replace(" PM", "p")
                        endLabel.BackColor = System.Drawing.Color.FromName("#FEEDA9")
                    End If

                    If app.Subject = "Preference" Then
                        Dim TimeFormat2 As String
                        TimeFormat2 = Trim("-" + app.[End].ToShortTimeString)
                        endLabelPref.Text = TimeFormat2.Replace(" AM", "a")
                        endLabelPref.Text = "-" + app.[End].ToShortTimeString.Replace(" PM", "p")
                        endLabelPref.BackColor = System.Drawing.Color.FromName("#D0ECBB")
                    End If
                Next
            End If
            Dim divisionLabel As New Label()
            divisionLabel.Text = " | "
            divisionLabel.BackColor = Color.Transparent
            e.TimeSlot.Control.Controls.Add(startLabel)
            e.TimeSlot.Control.Controls.Add(endLabel)
            e.TimeSlot.Control.Controls.Add(divisionLabel)
            e.TimeSlot.Control.Controls.Add(startLabelPref)
            e.TimeSlot.Control.Controls.Add(endLabelPref)
            ''e.Appointment.Attributes.Add("onclick", "javascript:ViewLeave('" & DataRowView.Item("LeaveID").ToString() & "');")
            startLabel.Attributes.Add("onclick", "TimeRangeOnclick")
        End If
        ''End If
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim ScheduleOption As String = DropDownList1.SelectedItem.ToString
        Dim Msgtext0 As String = ""
        Dim Msgtext1 As String = ""
        Dim Msgtext2 As String = ""
        Select Case ScheduleOption
            Case "Full Time 30 to 40 hours"
                Msgtext0 = "Max number of weekday mornings off is 1"
                Msgtext1 = "Max number of weekday evenings off is 1"
                Msgtext2 = "Max number of days off per week is 2"
                BulletedList1.Items(0).Text = Msgtext0
                BulletedList1.Items(1).Text = Msgtext1
                BulletedList1.Items(2).Text = Msgtext2
            Case "Part Time 15 to 25 hours"
                Msgtext0 = "Max number of weekday mornings off is 3"
                Msgtext1 = "Max number of weekday evenings off is 2"
                Msgtext2 = "Max number of days off per week is 4"
                BulletedList1.Items(0).Text = Msgtext0
                BulletedList1.Items(1).Text = Msgtext1
                BulletedList1.Items(2).Text = Msgtext2

        End Select
       
    End Sub

  
End Class

________________________________________________
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<Appointments>
  <NextID>69</NextID>
  <Appointment>
    <ID>52</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-10T11:00Z</Start>
    <End>2012-06-10T17:00Z</End>
  </Appointment>
  <Appointment>
    <ID>53</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-11T09:30Z</Start>
    <End>2012-06-11T18:00Z</End>
  </Appointment>
  <Appointment>
    <ID>54</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-12T13:30Z</Start>
    <End>2012-06-12T18:30Z</End>
  </Appointment>
  <Appointment>
    <ID>55</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-13T09:30Z</Start>
    <End>2012-06-13T13:00Z</End>
  </Appointment>
  <Appointment>
    <ID>56</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-14T15:00Z</Start>
    <End>2012-06-14T21:30Z</End>
  </Appointment>
  <Appointment>
    <ID>57</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-15T09:00Z</Start>
    <End>2012-06-15T13:00Z</End>
  </Appointment>
  <Appointment>
    <ID>58</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-10T10:30Z</Start>
    <End>2012-06-10T21:00Z</End>
    <TimeZoneID>UTC</TimeZoneID>
  </Appointment>
  <Appointment>
    <ID>59</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-11T09:30Z</Start>
    <End>2012-06-11T18:00Z</End>
    <TimeZoneID>UTC</TimeZoneID>
  </Appointment>
  <Appointment>
    <ID>60</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-12T09:00Z</Start>
    <End>2012-06-12T19:00Z</End>
  </Appointment>
  <Appointment>
    <ID>61</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-13T08:30Z</Start>
    <End>2012-06-13T18:30Z</End>
  </Appointment>
  <Appointment>
    <ID>62</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-14T13:00Z</Start>
    <End>2012-06-14T22:30Z</End>
  </Appointment>
  <Appointment>
    <ID>63</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-15T08:30Z</Start>
    <End>2012-06-15T16:00Z</End>
    <TimeZoneID>UTC</TimeZoneID>
  </Appointment>
  <Appointment>
    <ID>64</ID>
    <Subject>Availability</Subject>
    <Start>2012-06-16T11:00Z</Start>
    <End>2012-06-16T23:30Z</End>
  </Appointment>
  <Appointment>
    <ID>65</ID>
    <Subject>Preference</Subject>
    <Start>2012-06-16T13:30Z</Start>
    <End>2012-06-16T19:30Z</End>
  </Appointment>
</Appointments>

1 Answer, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 20 Jul 2012, 11:00 AM
Hi Allen,

 
You can refer to our Bind to Generic List on-line demo where similar functionality is shown.

Hope this will help you.

All the best,
Plamen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Scheduler
Asked by
Allen
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Share this question
or