Skip to main content

How to: Bind ASPxScheduler Resources to ObjectDataSource

  • 6 minutes to read

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.

Step 1. Create a Class to Store Resource Data

  1. 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.
  2. In the App_Code folder, create a new Class template, and name it CustomResource.
  3. 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).
  4. Create a list of CustomResource elements, and implement a method for the Select operation.

    using System.Collections;
    [Serializable]
    public class CustomResource
    {
        object resourceID;
        string name;
    
        public CustomResource() {
        }
    
        public CustomResource(object ID, string nameValue) {
            this.resourceID = ID;
            this.name = nameValue;
        }
    
    
        public string Name { get { return name; } set { name = value; } }
        public object ResourceID { get { return resourceID; } set { resourceID = value; } }
    }
    
    [Serializable]
    public class CustomResourceList : List<CustomResource>
    {
    }
    
    public class CustomResourceDataSource
    {
        CustomResourceList events;
        public CustomResourceDataSource(CustomResourceList events) {
            if (events == null)
                DevExpress.XtraScheduler.Native.Exceptions.ThrowArgumentNullException("resources");
            this.events = events;
        }
        public CustomResourceDataSource()
            : this(new CustomResourceList()) {
        }
        public CustomResourceList Resources { get { return events; } set { events = value; } }
        public int Count { get { return Resources.Count; } }
    
        public IEnumerable SelectMethodHandler() {
            CustomResourceList result = new CustomResourceList();
            result.AddRange(Resources);
            return result;
        }
    }
    
  5. 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.

Step 2. Create a Resources Object Data Source

  1. 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.
  2. Click the control’s smart tag, and click the Configure Data Source item.

    ConfigureDataSource.png

  3. In the Configure Data Source window, select CustomResourceDataSource as the business object. Click Next.

    ResourcesBusinessObject.png

  4. 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.

    ResourcesSelectMethod.png

  5. Next, bind the ASPxScheduler to the ObjectDataSource control by setting its ResourceDataSourceID property to resourceDataSource in the Property window.

Step 3. Set Up the Resource Mappings

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>

Step 4. Create a class to fill the Resources Data Source

  1. 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.

    using DevExpress.Web.ASPxScheduler;
    using DevExpress.XtraScheduler;
    public class ResourceHelper
    {
        public static string[] Users = new string[] { "Sarah Brighton", "Ryan Fischer", "Andrew Miller" };
        public static string[] Usernames = new string[] { "sbrighton", "rfischer", "amiller" };
    
        public static void FillObjectDataSourceWithResources(CustomResourceList list, int count) {
                for (int i = 1; i <= count; i++)
                {
                    list.Add(new CustomResource(Usernames[i-1], Users[i - 1]));
                }
        }
    }
    
  2. To group your appointments in the scheduler by resource, set the ASPxScheduler.GroupType property to Resource in the Property window.

Step 5. Implement methods for data binding

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;
}

Result.

Run the project. The image below shows the scheduler at runtime.

RunningScheduler.png