Back to Devexpress

Object Data Source in ASP.NET Web Forms

dashboard-401410-web-dashboard-integrate-dashboard-component-aspnet-web-forms-dashboard-control-register-default-data-sources-object-data-source.md

latest10.2 KB
Original Source

Object Data Source in ASP.NET Web Forms

  • Feb 27, 2023
  • 6 minutes to read

This topic shows how to add the DashboardObjectDataSource to an in-memory data source storage, and make it available to users.

Create an Object

In your application, create a class that returns a .NET object (for example, a typed list). The code below shows a list of invoices that are used as sample data:

cs
using System;
using System.Collections.Generic;

namespace WebFormsDashboardDataSources {
    public class Invoices {
        static Random rnd = new Random();
        public string Country { get; set; }
        public string City { get; set; }
        public string ProductName { get; set; }
        public DateTime OrderDate { get; set; }
        public int Quantity { get; set; }
        public double Discount { get; set; }
        public double ExtendedPrice { get; set; }
        public double Freigth { get; set; }
        public double UnitPrice { get; set; }

        public static List<Invoices> CreateData() {
            List<Invoices> data = new List<Invoices>();
            data.Add(new Invoices { Country = "Germany", City = "Aachen", ProductName = "Raclette Courdavault", OrderDate = GenerateOrderDate(), Quantity = 30, Discount = 0, ExtendedPrice = 1650, Freigth = 149.47, UnitPrice = 55 });
            data.Add(new Invoices { Country = "Germany", City = "Berlin", ProductName = "Raclette Courdavault", OrderDate = GenerateOrderDate(), Quantity = 15, Discount = 0, ExtendedPrice = 825, Freigth = 69.53, UnitPrice = 55 });
            data.Add(new Invoices { Country = "Germany", City = "Brandenburg", ProductName = "Raclette Courdavault", OrderDate = GenerateOrderDate(), Quantity = 61, Discount = 0, ExtendedPrice = 2959, Freigth = 42.33, UnitPrice = 99 });
            // ...
            return data;
        }

        static DateTime GenerateOrderDate() {
            int startYear = DateTime.Today.Year - 3;
            int endYear = DateTime.Today.Year;
            return new DateTime(rnd.Next(startYear, endYear), rnd.Next(1, 13), rnd.Next(1, 29));
        }
    }
}
vb
Imports System
Imports System.Collections.Generic

Namespace WebFormsDashboardDataSources

    Public Class Invoices

        Private Shared rnd As Random = New Random()

        Public Property Country As String

        Public Property City As String

        Public Property ProductName As String

        Public Property OrderDate As Date

        Public Property Quantity As Integer

        Public Property Discount As Double

        Public Property ExtendedPrice As Double
        ' ...
            data.Add(New Invoices With {.Country = "USA", .City = "Boise", .ProductName = "Raclette Courdavault", .OrderDate = GenerateOrderDate(), .Quantity = 40, .Discount = 0.15, .ExtendedPrice = 1496, .Freigth = 214.27, .UnitPrice = 44})
            data.Add(New Invoices With {.Country = "USA", .City = "Boise", .ProductName = "Raclette Courdavault", .OrderDate = GenerateOrderDate(), .Quantity = 7, .Discount = 0, .ExtendedPrice = 385, .Freigth = 8.19, .UnitPrice = 55})
            data.Add(New Invoices With {.Country = "USA", .City = "Boise", .ProductName = "Raclette Courdavault", .OrderDate = GenerateOrderDate(), .Quantity = 100, .Discount = 0.25, .ExtendedPrice = 4125, .Freigth = 830.75, .UnitPrice = 55})
            data.Add(New Invoices With {.Country = "USA", .City = "Boise", .ProductName = "Camembert Pierrot", .OrderDate = GenerateOrderDate(), .Quantity = 70, .Discount = 0, .ExtendedPrice = 2380, .Freigth = 896.77, .UnitPrice = 68})
            data.Add(New Invoices With {.Country = "USA", .City = "Boise", .ProductName = "Camembert Pierrot", .OrderDate = GenerateOrderDate(), .Quantity = 45, .Discount = 0.2, .ExtendedPrice = 1224, .Freigth = 487.57, .UnitPrice = 34})
            data.Add(New Invoices With {.Country = "USA", .City = "Eugene", .ProductName = "Raclette Courdavault", .OrderDate = GenerateOrderDate(), .Quantity = 30, .Discount = 0.15, .ExtendedPrice = 1402.5, .Freigth = 18.53, .UnitPrice = 55})
            data.Add(New Invoices With {.Country = "USA", .City = "Eugene", .ProductName = "Camembert Pierrot", .OrderDate = GenerateOrderDate(), .Quantity = 15, .Discount = 0, .ExtendedPrice = 510, .Freigth = 14.01, .UnitPrice = 34})
            data.Add(New Invoices With {.Country = "USA", .City = "Lander", .ProductName = "Camembert Pierrot", .OrderDate = GenerateOrderDate(), .Quantity = 20, .Discount = 0.2, .ExtendedPrice = 435.2, .Freigth = 30.96, .UnitPrice = 27.2})
            data.Add(New Invoices With {.Country = "USA", .City = "Portland", .ProductName = "Raclette Courdavault", .OrderDate = GenerateOrderDate(), .Quantity = 1, .Discount = 0, .ExtendedPrice = 55, .Freigth = 26.31, .UnitPrice = 55})

Configure an Object Data Source

For example, your ASPX page contains the ASPxDashboard control which unique identifier is ASPxDashboardObjectDS:

aspx
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="position: absolute; top: 0; bottom: 0; left: 0; right: 0">
            <dx:ASPxDashboard ID="ASPxDashboardObjectDS" runat="server" Width="100%" Height="100%">
            </dx:ASPxDashboard>
        </div>
    </form>
</body>
</html>

You can define the Object Data Source in the code-behind page that has the .aspx.cs or .aspx.vb extension depending on the language used:

Note

A code-behind page is one of the variants where you can register the data sources. For example, you can also register them in the Global.asax.cs (Global.asax.vb) file.

cs
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;

namespace WebFormsDashboardDataSources.Pages {
    public partial class ObjectDashboard : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        // ...
            // Create a data source storage.
            DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();

            // Register an Object data source.
            DashboardObjectDataSource objDataSource = new DashboardObjectDataSource("Object Data Source");
            objDataSource.DataId = "objectDataSource";
            dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml());

            // Set the configured data source storage.
            ASPxDashboardObjectDS.SetDataSourceStorage(dataSourceStorage);

            ASPxDashboardObjectDS.DataLoading += ASPxDashboardObjectDS_DataLoading;
            ASPxDashboardObjectDS.InitialDashboardId = "dashboardObjectDS";
        }

        private void ASPxDashboardObjectDS_DataLoading(object sender, DataLoadingWebEventArgs e) {
            if (e.DataId == "objectDataSource") {
                e.Data = Invoices.CreateData();
            }
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports System

Namespace WebFormsDashboardDataSources.Pages

    Public Partial Class ObjectDashboard
        Inherits Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' ...
            ' Create a data source storage.
            Dim dataSourceStorage As DataSourceInMemoryStorage = New DataSourceInMemoryStorage()
            ' Register an Object data source.
            Dim objDataSource As DashboardObjectDataSource = New DashboardObjectDataSource("Object Data Source")
            objDataSource.DataId = "objectDataSource"
            dataSourceStorage.RegisterDataSource("objDataSource", objDataSource.SaveToXml())
            ' Set the configured data source storage.
            ASPxDashboardObjectDS.SetDataSourceStorage(dataSourceStorage)
            AddHandler ASPxDashboardObjectDS.DataLoading, AddressOf Me.ASPxDashboardObjectDS_DataLoading
            ASPxDashboardObjectDS.InitialDashboardId = "dashboardObjectDS"
        End Sub

        Private Sub ASPxDashboardObjectDS_DataLoading(ByVal sender As Object, ByVal e As DataLoadingWebEventArgs)
            If Equals(e.DataId, "objectDataSource") Then
                e.Data = Invoices.CreateData()
            End If
        End Sub
    End Class
End Namespace

The Object Data Source is now available in the Web Dashboard:

Users can bind dashboard items to data in the Web Dashboard’s UI.

Example

The example shows how to make a set of data sources available for users in the Web Dashboard application.

View Example: How to Register Data Sources for ASP.NET Web Forms Dashboard Control