Manage the Dashboard State in ASP.NET Web Forms 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:
- ASPxDashboard.InitialDashboardState
- Gets or sets the initial dashboard state when loading a dashboard.
- ASPxDashboard.SetInitialDashboardState
- Allows you to specify 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 ASPxDashboard.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 ASPxDashboard.IncludeDashboardIdToUrl property to true
.
Note
These properties are in effect when the ASPxDashboard.WorkingMode is set to WorkingMode.Viewer or WorkingMode.ViewerOnly.
Apply a Dashboard State during Server-Side Export
You can use the ASPxDashboardExporter to apply a dashboard state during the server-side export. For this, use the overload of the following methods with the dashboardState parameter:
- WebDashboardExporter.ExportToPdf / WebDashboardExporter.ExportDashboardItemToPdf
- WebDashboardExporter.ExportToImage / WebDashboardExporter.ExportDashboardItemToImage
- WebDashboardExporter.ExportToExcel / WebDashboardExporter.ExportDashboardItemToExcel
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 ASPxDashboard.SetInitialDashboardState event is used to apply the specified dashboard state when loading a dashboard.
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
using System.Collections.Generic;
namespace WebDashboard_ManualDashboardState {
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void ASPxDashboard1_SetInitialDashboardState(object sender, SetInitialDashboardStateEventArgs e) {
e.InitialState = InitializeDashboardState();
}
public DashboardState InitializeDashboardState() {
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;
}
}
}
Example 2: Save a Dashboard State to Cookies
The sample illustrates how to save the current ASPxDashboard state (such as master filter or parameter values) to cookies on the client side and restore this state on the server side. The following API is used in this example:
On the server, the dashboard state is loaded from the cookies and applied to e.InitialState property to set the initial state of the loaded dashboard:
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
using System.Web;
namespace WebDashboard_DashboardStateCookies {
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void ASPxDashboard1_SetInitialDashboardState(object sender, SetInitialDashboardStateEventArgs e) {
// Requests the "dashboardState" cookie:
HttpCookie cookie = Request.Cookies["dashboardState"];
if (cookie != null) {
DashboardState dashboardState = new DashboardState();
// Initializes a DevExpress.DashboardCommon.DashboardState object from the JSON state:
dashboardState.LoadFromJson(HttpUtility.UrlDecode(cookie.Value));
if (e.DashboardId == "dashboard1")
// Applies the initial dashboard state if it exists in cookies:
e.InitialState = dashboardState;
}
}
}
}
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
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.