Skip to main content

ASPxDashboard.SetInitialDashboardState Event

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

Namespace: DevExpress.DashboardWeb

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

NuGet Package: DevExpress.Web.Dashboard

Declaration

public event SetInitialDashboardStateEventHandler SetInitialDashboardState

Event Data

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

Property Description
DashboardId Gets the identifier of the loaded dashboard.
DashboardXml Get the XML document containing the definition of the loaded dashboard.
InitialState Gets 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:

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

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

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

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:

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