dashboard-119765-web-dashboard-integrate-dashboard-component-aspnet-mvc-dashboard-extension-manage-dashboard-state.md
The dashboard state stores the state of individual dashboard items and dashboard parameter values. It can be master filter and drill-down values, active tab page, selected parameter values, and other results of user actions in a dashboard on the client.
On the server, the dashboard state is stored in a DashboardState class instance:
DashboardState dashboardState = new DashboardState();
DashboardParameterState parameterState =
new DashboardParameterState("countryParameter", "USA", typeof(string));
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>() {
treemapDrilldownState,
rangeFilterState }
);
Dim dashboardState As New DashboardState()
Dim parameterState As New DashboardParameterState("countryParameter", "USA", GetType(String))
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 {treemapDrilldownState, rangeFilterState})
Use the DashboardState.Items property to access the state of individual dashboard items:
DashboardItemState.MasterFilterValuesGets or sets selected master filter values stored in a dashboard state.DashboardItemState.DrillDownValues.DrillDownValuesGets or sets values for the drill-down hierarchy.DashboardItemState.TabPageNameGets the name of the tab page to which the dashboard item belongs.DashboardItemState.RangeFilterStateGets or sets the state of the Range Filter and Date Filter dashboard items.DashboardItemState.SelectedLayerIndexGets or sets an index of the selected dashboard item layer.
Use the DashboardState.Parameters property to access the selected dashboard parameter values.
You can use the following API to apply a DashboardState on the server:
DashboardExtensionSettings.InitialDashboardStateGets or sets the initial dashboard state when loading a dashboard.DashboardConfigurator.SetDashboardStateServiceSpecifies a service that allows you to manage a dashboard state.
See the Examples section for implementation details.
Users can share a dashboard state by appending this state to the URL of the currently browsed dashboard. Enable the DashboardExtensionSettings.IncludeDashboardStateToUrl property to display the state information in the URL. In this case, users can share a link to the dashboard with the applied state.
You can also add the identifier of the selected dashboard to the URL. For this, set the DashboardExtensionSettings.IncludeDashboardIdToUrl property to true.
Note
These properties are in effect when the DashboardExtensionSettings.WorkingMode is set to WorkingMode.Viewer or WorkingMode.ViewerOnly.
You can use the WebDashboardExporter to apply a dashboard state during the server-side export. For this, use the overload of the following methods with the dashboardState parameter:
For more information, see Manage Exporting Capabilities.
On the client, the dashboard state stores the same data: the state of individual dashboard items and dashboard parameter values. You can use the state as a JSON string or as a DashboardState object:
function setStateAsString() {
var currentState = `{"Items":{"listBoxDashboardItem1":{"MasterFilterValues":[["Beverages"]]},
"tabContainerDashboardItem1":{"TabPageName":"dashboardTabPage2"},
"cardDashboardItem1":{"SelectedLayerIndex":1}}}`
dashboardControl.GetDashboardControl().setDashboardState(currentState);
}
function setStateAsObject() {
dashboardControl.GetDashboardControl().setDashboardState({
"Parameters": {
"Year": 2008
},
"Items": {
"cardProductionImportByType": {
MasterFilterValues: [["Solid Fuels"]]
}
}
});
}
On the client, use the DashboardState.Items property to access the dashboard item’s state:
ItemState.MasterFilterValuesSpecifies the selected master filter values stored in a dashboard state.ItemState.DrillDownValuesSpecifies the current drill-down values.ItemState.TabPageNameSpecifies the name of the selected tab page in the Tab Container.ItemState.RangeFilterStateSpecifies the state of the Range Filter and Date Filter dashboard items.ItemState.SelectedLayerIndexSpecifies the index of the selected dashboard item layer for the Pie, Card, Treemap, Gauge, and Choropleth Map dashboard items.
Use the DashboardState.Parameters property to get access to the selected value of the dashboard parameters.
You can use the following API to apply the DashboardState on the client:
DashboardControlOptions.initialDashboardStateSpecifies the initial state of the loaded dashboard.DashboardControl.getDashboardStateGets the current dashboard state.DashboardControl.setDashboardStateApplies the dashboard state to the loaded dashboard.
The DashboardControlOptions.onDashboardStateChanged event occurs after the current dashboard state changes in the Web Dashboard. You can use this event to update the dashboard state value.
See the Examples section for implementation details.
To initialize the DashboardState object with the JSON object from the client, call the server-side DashboardStateExtensions.LoadFromJson method. Use DashboardStateExtensions.SaveToJson to save the current DashboardState object to JSON. Use the HttpRequest.Cookies property to get a collection of cookies sent by the client.
On the client, use the ASPxClientUtils.SetCookie method to create or update the HTTP cookie for the response. The document.cookie property reads and writes cookies associated with the document.
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 code below clears filters (unselects all master filter values), the RangeFilter state and drill-down levels for dashboard items. To do this, assign an empty array of objects to the ItemState.MasterFilterValues, ItemState.DrillDownValues and ItemState.RangeFilterState properties.
function onClick() {
//...
var state = JSON.parse(control.getDashboardState());
if (state && state.Items) {
Object.keys(state.Items).forEach(function (itemName) {
var itemState = state.Items[itemName];
itemState.MasterFilterValues = [];
itemState.DrillDownValues = [];
itemState.RangeFilterState = { };
})
}
control.setDashboardState(JSON.stringify(state));
}
Note
The control variable is the obtained DashboardControl instance. Refer to the Extensions Overview topic for information on how to obtain a DashboardControl for each platform.