Back to Devexpress

ASPxDashboard.SetInitialDashboardState Event

dashboard-devexpress-dot-dashboardweb-dot-aspxdashboard-21c636d5.md

latest12.7 KB
Original Source

ASPxDashboard.SetInitialDashboardState Event

Allows you to specify the initial dashboard state when loading a dashboard.

Namespace : DevExpress.DashboardWeb

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

NuGet Package : DevExpress.Web.Dashboard

Declaration

csharp
public event SetInitialDashboardStateEventHandler SetInitialDashboardState
vb
Public Event SetInitialDashboardState As SetInitialDashboardStateEventHandler

Event Data

The SetInitialDashboardState event's data class is SetInitialDashboardStateEventArgs. The following properties provide information specific to this event:

PropertyDescription
DashboardIdGets the identifier of the loaded dashboard.
DashboardXmlGet the XML document containing the definition of the loaded dashboard.
InitialStateGets or sets the initial state of the loaded dashboard.

Remarks

Use the SetInitialDashboardStateEventArgs.InitialState event parameter to specify the initial dashboard state. To learn more about a dashboard state, see Manage Dashboard State.

Note

The SetInitialDashboardState event is not raised if the ASPxDashboard control uses the DashboardConfigurator API.

To clear states (unselect all values) of dashboard items in the SetInitialDashboardState event, assign an empty array of objects to the DashboardItemState.MasterFilterValues property:

csharp
protected void ASPxDashboard1_SetInitialDashboardState(object sender, SetInitialDashboardStateEventArgs e) {
    var state = new DashboardState();
    // ...
    var itemState = new DashboardItemState("comboBoxDashboardItem");
    itemState.MasterFilterValues.Add(new object() { });
    state.Items.Add(itemState);
    // ...
    e.InitialState = state;
}
vb
Protected Sub ASPxDashboard1_SetInitialDashboardState(ByVal sender As Object, ByVal e As SetInitialDashboardStateEventArgs)
    Dim state As New DashboardState()
    ' ...
    Dim itemState As New DashboardItemState("comboBoxDashboardItem")
    itemState.MasterFilterValues.Add(New Object() { })
    state.Items.Add(itemState)
    ' ...
    e.InitialState = state
End Sub

Examples

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.

View Example: How to specify a default dashboard state in code

csharp
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;
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports System
Imports System.Collections.Generic

Namespace WebDashboard_ManualDashboardState

    Public Partial Class [Default]
        Inherits Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub

        Protected Sub ASPxDashboard1_SetInitialDashboardState(ByVal sender As Object, ByVal e As SetInitialDashboardStateEventArgs)
            e.InitialState = InitializeDashboardState()
        End Sub

        Public Function InitializeDashboardState() As DashboardState
            Dim dashboardState As DashboardState = New DashboardState()
            Dim parameterState As DashboardParameterState = New DashboardParameterState("countryParameter", "USA", GetType(String))
            Dim gridFilterState As DashboardItemState = New DashboardItemState("gridDashboardItem1")
            gridFilterState.MasterFilterValues.AddRange(New List(Of Object())() From {New String(0) {"Andrew Fuller"}, New String(0) {"Laura Callahan"}})
            Dim treemapDrilldownState As DashboardItemState = New DashboardItemState("treemapDashboardItem1")
            treemapDrilldownState.DrillDownValues.Add("Beverages")
            Dim rangeFilterState As DashboardItemState = 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(Of DashboardItemState)() From {gridFilterState, treemapDrilldownState, rangeFilterState})
            Return dashboardState
        End Function
    End Class
End Namespace

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:

View Example: How to save a dashboard state to cookies

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:

csharp
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;
            }
        }
    }
}
vb
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports System
Imports System.Web

Namespace WebDashboard_DashboardStateCookies

    Public Partial Class [Default]
        Inherits UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub

        Protected Sub ASPxDashboard1_SetInitialDashboardState(ByVal sender As Object, ByVal e As SetInitialDashboardStateEventArgs)
            ' Requests the "dashboardState" cookie:
            Dim cookie As HttpCookie = Request.Cookies("dashboardState")
            If cookie IsNot Nothing Then
                Dim dashboardState As DashboardState = New DashboardState()
                ' Initializes a DevExpress.DashboardCommon.DashboardState object from the JSON state:
                dashboardState.LoadFromJson(HttpUtility.UrlDecode(cookie.Value))
                ' Applies the initial dashboard state if it exists in cookies:
                If Equals(e.DashboardId, "dashboard1") Then e.InitialState = dashboardState
            End If
        End Sub
    End Class
End Namespace

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=/";
}
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebDashboard_DashboardStateCookies.Default" %>

<%@ Register Assembly="DevExpress.Dashboard.v24.2.Web.WebForms, Version=24.2.13.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" 
    Namespace="DevExpress.DashboardWeb" TagPrefix="dx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Web Dashboard Application</title>
    <script src="Script.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="position:absolute; left:0; right:0; top:0; bottom:0;">
        <dx:ASPxDashboard ID="ASPxDashboard1" runat="server" 
            WorkingMode="ViewerOnly" 
            ClientInstanceName="webDashboard"
            IncludeDashboardIdToUrl="true"
            DashboardStorageFolder="~/App_Data/Dashboards" 
            Height="100%" Width="100%" 
            OnSetInitialDashboardState="ASPxDashboard1_SetInitialDashboardState">
            <ClientSideEvents BeforeRender="onBeforeRender"></ClientSideEvents>
        </dx:ASPxDashboard>
    </div>
    </form>
</body>
</html>

See Also

ASPxDashboard Class

ASPxDashboard Members

DevExpress.DashboardWeb Namespace