Back to Devexpress

ASPxGridBase.ClientLayout Event

aspnet-devexpress-dot-web-dot-aspxgridbase-7a82c798.md

latest7.7 KB
Original Source

ASPxGridBase.ClientLayout Event

Enables you to save and restore the previously saved layout of the grid.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event ASPxClientLayoutHandler ClientLayout
vb
Public Event ClientLayout As ASPxClientLayoutHandler

Event Data

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

PropertyDescription
LayoutDataGets or sets the layout data.
LayoutModeIndicates whether a control’s layout should be saved or restored.

Remarks

Handle the ClientLayout event to save and restore the grid’s layout from a data store.

Save Layout

Restore Layout

You can also save and restore the grid’s layout via the ASPxGridBase.SaveClientLayout and ASPxGridBase.LoadClientLayout methods.

Concept

Save and Restore Layout

Example: How to save or restore the ASPxGridView layout to or from a data store

csharp
protected void ASPxGridView1_ClientLayout(object sender, 
DevExpress.Web.ASPxClientLayoutArgs e) {
    if (e.LayoutMode == DevExpress.Web.ClientLayoutMode.Saving) {
        SaveUserLayoutToDatabase(userID, "AccountGrid", e.LayoutData);
    }
    else {
        if (System.IO.File.Exists(fileName))
            e.LayoutData = RestoreUserLayoutFromDatabase(userID, "AccountGrid");
    }
}
vb
Protected Sub ASPxGridView1_ClientLayout(sender As Object,_
e As DevExpress.Web.ASPxClientLayoutArgs)
   If e.LayoutMode = DevExpress.Web.ClientLayoutMode.Saving Then
      SaveUserLayoutToDatabase(userID, "AccountGrid", e.LayoutData)
   Else
      If File.Exists(fileName) Then
         e.LayoutData = RestoreUserLayoutFromDatabase(userID, "AccountGrid")
      End If
   End If
End Sub

Example: How to keep the detail grids state after a master ASPxGridView layout is changed

aspx
<dx:ASPxGridView ID="MasterGrid" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
    KeyFieldName="CategoryID" OnDetailRowExpandedChanged="MasterGrid_DetailRowExpandedChanged">
    <Columns>
        <dx:GridViewCommandColumn ShowClearFilterButton="True"/>
        <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" >
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="CategoryName" />
        <dx:GridViewDataTextColumn FieldName="Description" />
    </Columns>
    <Settings ShowFilterRow="True" ShowGroupPanel="True" />
    <SettingsDetail ShowDetailRow="True" />
    <Templates>
        <DetailRow>
            <dx:ASPxGridView ID="DetailGrid" runat="server" AutoGenerateColumns="False" 
                DataSourceID="SqlDataSource2" KeyFieldName="ProductID" 
                OnBeforePerformDataSelect="DetailGrid_BeforePerformDataSelect"
                OnClientLayout="DetailGrid_ClientLayout">
                <Columns>
                    <dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" >
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn FieldName="ProductName" />
                    <dx:GridViewDataTextColumn FieldName="CategoryID" />
                    <dx:GridViewDataTextColumn FieldName="UnitPrice" />
                </Columns>
            </dx:ASPxGridView>
        </DetailRow>
    </Templates>
</dx:ASPxGridView>
csharp
protected void Page_Init(object sender, EventArgs e) {
    if (!IsPostBack)
        Session.Clear();
}
protected void DetailGrid_BeforePerformDataSelect(object sender, EventArgs e) {
    Session["CategoryID"] = ((ASPxGridView)sender).GetMasterRowKeyValue();
}
protected void DetailGrid_ClientLayout(object sender, DevExpress.Web.ASPxClientLayoutArgs e) {
    ASPxGridView grid = sender as ASPxGridView;
    string key = grid.GetMasterRowKeyValue().ToString();
    if (e.LayoutMode == DevExpress.Web.ClientLayoutMode.Loading && Session["DetailGrid"+key] != null ) {
        e.LayoutData = (string)Session["DetailGrid"+key];
    }
    else {
        Session["DetailGrid"+key] = e.LayoutData;
    }

}
protected void MasterGrid_DetailRowExpandedChanged(object sender, ASPxGridViewDetailRowEventArgs e) {
    if (e.Expanded == false) {
        string key = MasterGrid.GetRowValues(e.VisibleIndex, MasterGrid.KeyFieldName).ToString();
        Session["DetailGrid" + key] = null;  
    }
}
vb
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    If (Not IsPostBack) Then
    Session.Clear()
    End If
End Sub

Protected Sub DetailGrid_BeforePerformDataSelect(ByVal sender As Object, ByVal e As EventArgs)
  Session("CategoryID") = (CType(sender, ASPxGridView)).GetMasterRowKeyValue()
End Sub

Protected Sub DetailGrid_ClientLayout(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxClientLayoutArgs)
  Dim grid As ASPxGridView = TryCast(sender, ASPxGridView)
  Dim key As String = grid.GetMasterRowKeyValue().ToString()
  If e.LayoutMode = DevExpress.Web.ClientLayoutMode.Loading AndAlso Session("DetailGrid" & key) IsNot Nothing Then
    e.LayoutData = CStr(Session("DetailGrid" & key))
  Else
    Session("DetailGrid" & key) = e.LayoutData
  End If
End Sub

Protected Sub MasterGrid_DetailRowExpandedChanged(ByVal sender As Object, ByVal e As ASPxGridViewDetailRowEventArgs)
  If e.Expanded = False Then
    Dim key As String = MasterGrid.GetRowValues(e.VisibleIndex, MasterGrid.KeyFieldName).ToString()
    Session("DetailGrid" & key) = Nothing
  End If
End Sub

See Also

SaveClientLayout()

LoadClientLayout(String)

ASPxCardView.SettingsCookies

ASPxGridView.SettingsCookies

ASPxVerticalGrid.SettingsCookies

ASPxGridBase Class

ASPxGridBase Members

DevExpress.Web Namespace