Skip to main content

Manage Dashboard State in ASP.NET MVC Applications

  • 9 minutes to read

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.

Manage the Dashboard State on the Server

Dashboard State Object

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

States of Individual Dashboard Items

Use the DashboardState.Items property to access the state of individual dashboard items:

DashboardItemState.MasterFilterValues
Gets or sets selected master filter values stored in a dashboard state.
DashboardItemState.DrillDownValues.DrillDownValues
Gets or sets values for the drill-down hierarchy.
DashboardItemState.TabPageName
Gets the name of the tab page to which the dashboard item belongs.
DashboardItemState.RangeFilterState
Gets or sets the state of the Range Filter and Date Filter dashboard items.
DashboardItemState.SelectedLayerIndex
Gets or sets an index of the selected dashboard item layer.

Dashboard Parameter Values

Use the DashboardState.Parameters property to access the selected dashboard parameter values.

Manage a Dashboard State

You can use the following API to apply a DashboardState on the server:

DashboardExtensionSettings.InitialDashboardState
Gets or sets the initial dashboard state when loading a dashboard.
DashboardConfigurator.SetDashboardStateService
Specifies a service that allows you to manage a dashboard state.

See the Examples section for implementation details.

Add a Dashboard State to a URL

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.

Apply a Dashboard State during Server-Side Export

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.

Manage a Dashboard State on the Client

Dashboard State Object

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"]]
      }
    }
  });
}

States of Individual Dashboard Items

On the client, use the DashboardState.Items property to access the dashboard item’s state:

ItemState.MasterFilterValues
Specifies the selected master filter values stored in a dashboard state.
ItemState.DrillDownValues
Specifies the current drill-down values.
ItemState.TabPageName
Specifies the name of the selected tab page in the Tab Container.
ItemState.RangeFilterState
Specifies the state of the Range Filter and Date Filter dashboard items.
ItemState.SelectedLayerIndex
Specifies the index of the selected dashboard item layer for the Pie, Card, Treemap, Gauge, and Choropleth Map dashboard items.

Dashboard Parameter Values

Use the DashboardState.Parameters property to get access to the selected value of the dashboard parameters.

Manage a Dashboard State

You can use the following API to apply the DashboardState on the client:

DashboardControlOptions.initialDashboardState
Specifies the initial state of the loaded dashboard.
DashboardControl.getDashboardState
Gets the current dashboard state.
DashboardControl.setDashboardState
Applies 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.

Client-Server Interaction

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.

Examples

Example 1: 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:

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

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

Example 2: 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:

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

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

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()

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=/";
}

Example 3: Reset a Dashboard State to Default

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.