dashboard-devexpress-dot-dashboardweb-dot-dashboardconfigurator-dot-setdashboardstateservice-x28-devexpress-dot-dashboardweb-dot-idashboardstateservice-x29.md
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
public void SetDashboardStateService(
IDashboardStateService service
)
Public Sub SetDashboardStateService(
service As IDashboardStateService
)
| Name | Type | Description |
|---|---|---|
| service | IDashboardStateService |
An object implementing the IDashboardStateService interface that specifies a service allowing you to manage a dashboard state.
|
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.
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:
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;
}
}
}
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:
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());
}
}
}
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
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.
On the server, create a custom dashboard state service so you can load the dashboard state from the cookies:
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;
}
}
}
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:
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());
}
}
}
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:
@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()
@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.
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.
configurator.SetDashboardStateService(new CustomDashboardStateService(contextAccessor));
configurator.SetDashboardStateService(new CustomDashboardStateService());
DashboardConfigurator.Default.SetDashboardStateService(new CustomDashboardStateService());
}
DashboardConfigurator.Default.SetDashboardStateService(New CustomDashboardStateService())
End Sub
See Also