aspnet-15685-components-scheduler-examples-data-binding-how-to-bind-aspxscheduler-resources-to-objectdatasource.md
This document demonstrates how to add Resources to the ASPxScheduler control. There are two methods to achieve this. The simplest one is to create a Resource object instance and add it directly to the ASPxResourceStorage. This method, however, has a disadvantage - you will need to use the DataBind method of the ASPxScheduler to create a new resource collection from the resource data source after the application is initialized. Thus, the performance of your application will suffer from additional data requests. The preferred method is to implement another ObjectDataSource component for resources in the same manner as it was done for appointments in the previous How to: Bind ASPxScheduler Appointment to ObjectDataSource topic.
The steps to implement this component are as follows.
Open the project containing the ASPxScheduler and the Appointments object data source, which was created in the previous How to: Bind ASPxScheduler Appointment to ObjectDataSource topic.
In the App_Code folder, create a new Class template, and name it CustomResource.
Define the class that will contain the necessary properties for the resources in the ASPxSchedulerStorage.Resources. The minimum requirement is to provide a unique identifier and a caption value (the name of the resource).
Create a list of CustomResource elements, and implement a method for the Select operation.
To associate an appointment with a resource, modify the CustomEvent class in the following way. Create a resourceID field of the Object type and implement the ResourceID property.
Switch to the Design View of the Default.aspx page, and drag the ObjectDataSource control from the Data Toolbox window to the page. Change the ID property of the control to resourceDataSource.
Click the control’s smart tag, and click the Configure Data Source item.
In the Configure Data Source window, select CustomResourceDataSource as the business object. Click Next.
In the next panel, select the SelectMethodHandler method from the Choose a method drop-down list. Leave the methods in the other tabs empty, since they are not required, and not implemented in the CustomResourceDataSource class.
Next, bind the ASPxScheduler to the ObjectDataSource control by setting its ResourceDataSourceID property to resourceDataSource in the Property window.
Next, set up the mapping for the Resource field in the ASPxSchedulerStorage. To do this, locate the < dx: ASPxScheduler > opening and closing tags in the Default.aspx file. Correct your code so that it resembles the following.
<dx:ASPxScheduler ID="ASPxScheduler1" runat="server" AppointmentDataSourceID="appointmentDataSource" ClientIDMode="AutoID"
OnAppointmentRowInserted="ASPxScheduler1_AppointmentRowInserted" ResourceDataSourceID="resourceDataSource" GroupType="Resource">
<Storage>
<Appointments>
<Mappings AppointmentId="Id" Start="StartTime" End="EndTime" Subject="Subject" AllDay="AllDay"
Description="Description" Label="Label" Location="Location" RecurrenceInfo="RecurrenceInfo"
ReminderInfo="ReminderInfo" Status="Status" Type="EventType" ResourceId ="ResourceID" />
</Appointments>
<Resources>
<Mappings ResourceId="ResourceID" Caption ="Name"/>
</Resources>
</Storage>
</dx:ASPxScheduler>
In the App_Code folder, create a new Class template, and name it ResourceHelper. The FillObjectDataSourceWithResources method demonstrates how to use an array of strings to populate the Resources data source.
To group your appointments in the scheduler by resource, set the ASPxScheduler.GroupType property to Resource in the Property window.
In the Default.aspx code file, add the ObjectDataSource’s ObjectCreated event handler, which will create an instance of the custom data source ( CustomResourceDataSource ), and initialize it with the CustomResourceList object.
CustomResourceDataSource objectResourceInstance;
protected void resourceDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
this.objectResourceInstance = new CustomResourceDataSource(GetCustomResources());
e.ObjectInstance = this.objectResourceInstance;
}
CustomResourceList GetCustomResources()
{
CustomResourceList resources = Session["CustomResourceListData"] as CustomResourceList;
if (resources == null) {
resources = new CustomResourceList();
ResourceHelper.FillObjectDataSourceWithResources(resources, 3);
Session["CustomResourceListData"] = resources;
}
return resources;
}
Private objectResourceInstance As CustomResourceDataSource
Protected Sub resourceDataSource_ObjectCreated(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs)
Me.objectResourceInstance = New CustomResourceDataSource(GetCustomResources())
e.ObjectInstance = Me.objectResourceInstance
End Sub
Private Function GetCustomResources() As CustomResourceList
Dim resources As CustomResourceList = TryCast(Session("CustomResourceListData"), CustomResourceList)
If resources Is Nothing Then
resources = New CustomResourceList()
ResourceHelper.FillObjectDataSourceWithResources(resources, 3)
Session("CustomResourceListData") = resources
End If
Return resources
End Function
Run the project. The image below shows the scheduler at runtime.