Back to Devexpress

DashboardConfigurator.SetDashboardStateService(IDashboardStateService) Method

dashboard-devexpress-dot-dashboardweb-dot-dashboardconfigurator-dot-setdashboardstateservice-x28-devexpress-dot-dashboardweb-dot-idashboardstateservice-x29.md

latest15.3 KB
Original Source

DashboardConfigurator.SetDashboardStateService(IDashboardStateService) Method

Specifies a service that allows you to manage a dashboard state.

Namespace : DevExpress.DashboardWeb

Assembly : DevExpress.Dashboard.v25.2.Web.dll

NuGet Package : DevExpress.Web.Dashboard.Common

Declaration

csharp
public void SetDashboardStateService(
    IDashboardStateService service
)
vb
Public Sub SetDashboardStateService(
    service As IDashboardStateService
)

Parameters

NameTypeDescription
serviceIDashboardStateService

An object implementing the IDashboardStateService interface that specifies a service allowing you to manage a dashboard state.

|

Remarks

Tip

For information on how to use the DashboardConfigurator‘s API, see the following topic: Server-Side API Overview.

For more information about a dashboard state, see the following topic: Manage Dashboard State in ASP.NET MVC Applications.

Examples

How to: Specify a Default Dashboard State

The sample illustrates how to specify a dashboard state (such as master filter or parameter values) in code and how to apply this state when loading a dashboard for the first time. In this example, the DashboardState object holds the required dashboard state. The DashboardConfigurator.SetDashboardStateService method is used to apply the specified dashboard state when loading a dashboard.

Create a custom dashboard state service and define the dashboard state:

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

namespace MvcDashboard_DefaultDashboardState {
    public class CustomDashboardStateService : IDashboardStateService {
        public DashboardState GetState(string dashboardId, System.Xml.Linq.XDocument dashboard) {
            DashboardState dashboardState = new DashboardState();

            DashboardParameterState parameterState =
                new DashboardParameterState("countryParameter", "USA", typeof(string));

            DashboardItemState gridFilterState = new DashboardItemState("gridDashboardItem1");
            gridFilterState.MasterFilterValues.AddRange(new List<object[]>() {
                new string[1] { "Andrew Fuller" },
                new string[1] { "Laura Callahan" }
            }
            );

            DashboardItemState treemapDrilldownState = new DashboardItemState("treemapDashboardItem1");
            treemapDrilldownState.DrillDownValues.Add("Beverages");

            DashboardItemState rangeFilterState = new DashboardItemState("rangeFilterDashboardItem1");
            rangeFilterState.RangeFilterState.Selection =
                new RangeFilterSelection(new DateTime(2015, 1, 1), new DateTime(2016, 1, 1));

            dashboardState.Parameters.Add(parameterState);
            dashboardState.Items.AddRange(new List<DashboardItemState>() {
                gridFilterState,
                treemapDrilldownState,
                rangeFilterState }
            );
            return dashboardState;
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb

Namespace MvcDashboard_DefaultDashboardState
    Public Class CustomDashboardStateService
        Implements IDashboardStateService

        Public Function GetState(ByVal dashboardId As String, ByVal dashboard As XDocument) As DashboardState Implements IDashboardStateService.GetState
            Dim dashboardState As New DashboardState()

            Dim parameterState As New DashboardParameterState("countryParameter", "USA", GetType(String))

            Dim gridFilterState As New DashboardItemState("gridDashboardItem1")
            gridFilterState.MasterFilterValues.AddRange(New List(Of Object())() From {
                New String(0) {"Andrew Fuller"},
                New String(0) {"Laura Callahan"}
            })

            Dim treemapDrilldownState As New DashboardItemState("treemapDashboardItem1")
            treemapDrilldownState.DrillDownValues.Add("Beverages")

            Dim rangeFilterState As New DashboardItemState("rangeFilterDashboardItem1")
            rangeFilterState.RangeFilterState.Selection = New RangeFilterSelection(New Date(2015, 1, 1), New Date(2016, 1, 1))

            dashboardState.Parameters.Add(parameterState)
            dashboardState.Items.AddRange(New List(Of DashboardItemState)() From {gridFilterState, treemapDrilldownState, rangeFilterState})
            Return dashboardState
        End Function
    End Class
End Namespace

Call the DashboardConfigurator.SetDashboardStateService(IDashboardStateService) method to apply the created dashboard state service:

cs
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using System.Web.Routing;

namespace MvcDashboard_DefaultDashboardState {
    public static class DashboardConfig {
        public static void RegisterService(RouteCollection routes) {
            routes.MapDashboardRoute("dashboardControl","DefaultDashboard");
            DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
            DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage);

            DashboardConfigurator.Default.SetDashboardStateService(new CustomDashboardStateService());
        }
    }
}
vb
Imports System.Web.Routing
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc

Namespace MvcDashboard_DefaultDashboardState
    Public NotInheritable Class DashboardConfig

        Private Sub New()
        End Sub

        Public Shared Sub RegisterService(ByVal routes As RouteCollection)
            routes.MapDashboardRoute("dashboardControl","DefaultDashboard")
            Dim dashboardFileStorage As New DashboardFileStorage("~/App_Data/Dashboards")
            DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage)

            DashboardConfigurator.Default.SetDashboardStateService(New CustomDashboardStateService())
        End Sub
    End Class
End Namespace

How to: Save a Dashboard State to Cookies

The sample illustrates how to save the current ASP.NET MVC Dashboard state (such as master filter or parameter values) to cookies on the client side and restore this state on the server side.

View Example: ASP.NET MVC

On the server, create a custom dashboard state service so you can load the dashboard state from the cookies:

cs
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System.Web;
using System.Xml.Linq;

namespace MvcDashboard_DashboardStateCookies {
    internal class CustomDashboardStateService : IDashboardStateService {
        public DashboardState GetState(string dashboardId, XDocument dashboard) {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["dashboardState"];
            if (cookie != null) {
                DashboardState dashboardState = new DashboardState();
                dashboardState.LoadFromJson(HttpUtility.UrlDecode(cookie.Value));
                return dashboardState;
            }
            else
                return null;
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb

Namespace MvcDashboard_DashboardStateCookies
    Friend Class CustomDashboardStateService
        Implements IDashboardStateService

        Public Function GetState(ByVal dashboardId As String, ByVal dashboard As XDocument) As DashboardState Implements IDashboardStateService.GetState
            Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies("dashboardState")
            If cookie IsNot Nothing Then
                Dim dashboardState As New DashboardState()
                dashboardState.LoadFromJson(HttpUtility.UrlDecode(cookie.Value))
                Return dashboardState
            Else
                Return Nothing
            End If
        End Function
    End Class
End Namespace

Call the DashboardConfigurator.SetDashboardStateService(IDashboardStateService) method to apply the created dashboard state service:

cs
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using System.Web.Routing;

namespace MvcDashboard_DashboardStateCookies {
    public static class DashboardConfig {
        public static void RegisterService(RouteCollection routes) {
            routes.MapDashboardRoute("dashboardControl", "DefaultDashboard");

            DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
            DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage);

            DashboardConfigurator.Default.SetDashboardStateService(new CustomDashboardStateService());
        }
    }
}
vb
Imports System.Web.Routing
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc

Namespace MvcDashboard_DashboardStateCookies
    Public NotInheritable Class DashboardConfig

        Private Sub New()
        End Sub

        Public Shared Sub RegisterService(ByVal routes As RouteCollection)
            routes.MapDashboardRoute("dashboardControl", "DefaultDashboard")

            Dim dashboardFileStorage As New DashboardFileStorage("~/App_Data/Dashboards")
            DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage)

            DashboardConfigurator.Default.SetDashboardStateService(New CustomDashboardStateService())
        End Sub
    End Class
End Namespace

Handle the DashboardControlOptions.onBeforeRender event to apply the dashboard state changes:

cshtml
@Html.DevExpress().Dashboard(settings => {
    settings.Name = "Dashboard";
    settings.ControllerName = "DefaultDashboard";
    settings.WorkingMode = DevExpress.DashboardWeb.WorkingMode.ViewerOnly;
    settings.Width = Unit.Percentage(100);
    settings.Height = Unit.Percentage(100);
    settings.ClientSideEvents.BeforeRender = "onBeforeRender";
}).GetHtml()
vbhtml
@Html.DevExpress().Dashboard(Sub(settings)
    settings.Name = "Dashboard"
    settings.ControllerName = "DefaultDashboard"
    settings.WorkingMode = DevExpress.DashboardWeb.WorkingMode.ViewerOnly
    settings.Width = Unit.Percentage(100)
    settings.Height = Unit.Percentage(100)
    settings.ClientSideEvents.BeforeRender = "onBeforeRender"
End Sub).GetHtml()

On the client, the control saves the dashboard state to cookies every time the state changes.

js
function onBeforeRender(sender) {
    const dashboardControl = sender.GetDashboardControl();
    if (dashboardControl)
        dashboardControl.on('dashboardStateChanged', onDashboardStateChanged);
}

function onDashboardStateChanged(e) {
    // Set the number of days until the cookie should expire (exdays):
    const exdays = 1;
    const date = new Date();
    date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires=" + date.toUTCString();
    // Get the dashboard state:
    let dState = "dashboardState=" + e.component.getDashboardState() + ";";
    // Assign the cookie name (dashboardState), the cookie value, and the expires string to document.cookie:
    document.cookie = dState + expires + ";path=/";
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the SetDashboardStateService(IDashboardStateService) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

asp-net-core-dashboard-save-dashboard-state-to-cookies/CS/WebDashboardAspNetCore/Code/DashboardUtils.cs#L21

csharp
configurator.SetDashboardStateService(new CustomDashboardStateService(contextAccessor));

asp-net-core-dashboard-update-parameter-master-filter-changed/CS/AspNetCoreDashboard/Code/DashboardUtils.cs#L20

csharp
configurator.SetDashboardStateService(new CustomDashboardStateService());

mvc-dashboard-save-dashboard-state-to-cookies/CS/MvcDashboard_DashboardStateCookies/App_Start/DashboardConfig.cs#L13

csharp
DashboardConfigurator.Default.SetDashboardStateService(new CustomDashboardStateService());
}

mvc-dashboard-save-dashboard-state-to-cookies/VB/MvcDashboard_DashboardStateCookies/App_Start/DashboardConfig.vb#L17

vb
DashboardConfigurator.Default.SetDashboardStateService(New CustomDashboardStateService())
End Sub

See Also

DashboardConfigurator Class

DashboardConfigurator Members

DevExpress.DashboardWeb Namespace