I am looking at this example.
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/savinggridsettingsonperuserbasis/defaultvb.aspx
I do not want the user to have to click any type of "Save" button. I just want the filters the user applied to a grid to be available the next time they come back to this page. What would be best way to go about this be?
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/savinggridsettingsonperuserbasis/defaultvb.aspx
I do not want the user to have to click any type of "Save" button. I just want the filters the user applied to a grid to be available the next time they come back to this page. What would be best way to go about this be?
12 Answers, 1 is accepted
0

Jayesh Goyani
Top achievements
Rank 2
answered on 07 Dec 2011, 05:49 PM
Hello,
or
Thanks,
Jayesh Goyani
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
string
user = UserSelection.SelectedValue;
GridSettingsPersister SavePersister =
new
GridSettingsPersister(RadGrid1);
Session[user] = SavePersister.SaveSettings();
}
or
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
if
(Session[
"Storesetting"
] !=
null
)
{
if
(Convert.ToBoolean(Session[
"Storesetting"
]))
{
string
user = UserSelection.SelectedValue;
GridSettingsPersister SavePersister =
new
GridSettingsPersister(RadGrid1);
Session[user] = SavePersister.SaveSettings();
Session[
"Storesetting"
] =
null
;
}
}
}
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.FilterCommandName)
{
Session[
"Storesetting"
] =
true
;
}
}
Thanks,
Jayesh Goyani
0

Ryan
Top achievements
Rank 1
answered on 07 Dec 2011, 06:17 PM
Which radgrid event would you reload the filter when you return to the page.
Also I I have multiple pages with radgrids that I will want to add this functionality to... I will just store the session as the something like "pagename_filter". Sound ok?
Also I I have multiple pages with radgrids that I will want to add this functionality to... I will just store the session as the something like "pagename_filter". Sound ok?
0

Ryan
Top achievements
Rank 1
answered on 07 Dec 2011, 06:44 PM
Also, the radgrid prerender does not even seem to get fired.
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
Dim SavePersister As New GridSettingsPersister(RadGrid1)
Session("qa_home_filter") = SavePersister.SaveSettings()
End Sub
0

Ryan
Top achievements
Rank 1
answered on 08 Dec 2011, 07:23 PM
Anyone?
0

Jayesh Goyani
Top achievements
Rank 2
answered on 09 Dec 2011, 07:10 AM
Hello Ryan,
Can you please provide your requirement so i can give you working code to you.
Thanks.
Jayesh Goyani
Can you please provide your requirement so i can give you working code to you.
Thanks.
Jayesh Goyani
0

Ryan
Top achievements
Rank 1
answered on 12 Dec 2011, 04:02 PM
When the user filters my grid, then navigates away via a link in the grid. They will then be returned to the page with the grid. The user does not want to have to re-filter the grid.
0

brian
Top achievements
Rank 1
answered on 12 Dec 2011, 04:35 PM
I'm not sure you want to save the settings in the PreRender. I'd save them when either leaving the page or in a method that handles various events (such as PageIndexChanged, SortChanged, GroupingChanged, etc.).
You don't have to do it in PreRender (did you register that event with the grid properties?).
I do load the setting in Page_Load.
You don't have to do it in PreRender (did you register that event with the grid properties?).
I do load the setting in Page_Load.
0

Ryan
Top achievements
Rank 1
answered on 12 Dec 2011, 06:50 PM
How do you Register the prerender?
I have this bit of code, but it never gets executed.
I have this bit of code, but it never gets executed.
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
Dim SavePersister As New GridSettingsPersister(RadGrid1)
Session("qa_home_filter") = SavePersister.SaveSettings()
End Sub
0

Ryan
Top achievements
Rank 1
answered on 12 Dec 2011, 07:13 PM
I got the pre render to to work by adding "Handles RadGrid1.PreRender" however, not sure if this is the best place to set the session.
0

brian
Top achievements
Rank 1
answered on 12 Dec 2011, 07:20 PM
OnPreRender="grdAccountsA_PreRender"
Well, since I'm playing with this right now, I was wrong. It seems it's best to Save on grid_PreRender and also if you leave the page (which I do via the ItemCommand).
I Set them during Page_Load
if (!IsPostBack)
{
SetGridSettings();
In my opinion, there are better ways but it needs too much time to figure out.
Also, the GridSettingsPersister... it's not 100% complete. Maybe there are variations floating around. It didn't have PageIndex or AllowFiltering
using
System;
using
System.Collections.Generic;
using
Telerik.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI;
using
System.IO;
namespace
***Web.Controls
{
public
class
GridSettingsPersister
{
/// <summary>
/// Initializes an instance of GridSettingsPersister from a RadGrid instance
/// </summary>
/// <param name="grid">The RadGrid instance to import and exports settings</param>
public
GridSettingsPersister(RadGrid grid)
:
this
(grid, GridSettingsType.All)
{
}
/// <summary>
/// Initializes an instance of GridSettingsPersister from a RadGrid instance
/// and a collection GridSettingsType values
/// </summary>
/// <param name="grid">The RadGrid instance to import and exports settings</param>
/// <param name="persistedSettings">
/// A collection of GridSettingType values specifying the type of grid settings
/// to import or export
/// </param>
public
GridSettingsPersister(RadGrid grid, GridSettingsType persistedSettingFlags)
{
_grid = grid;
_persistedSettingTypes = persistedSettingFlags;
_settings =
new
GridSettingsCollection();
_settings.ColumnSettings =
new
List<ColumnSettings>();
_settings.AutoGeneratedColumnSettings =
new
List<ColumnSettings>();
}
private
RadGrid _grid;
private
GridSettingsType _persistedSettingTypes;
private
GridSettingsCollection _settings;
/// <summary>
/// The underlyiong RadGrid instance to import or export settings from
/// </summary>
public
RadGrid Grid
{
get
{
return
_grid; }
}
/// <summary>
/// Gets or sets the GridSettingType flags that specify the grid settings to
/// export or import
/// </summary>
public
virtual
GridSettingsType PersistedSettingTypes
{
get
{
return
_persistedSettingTypes; }
set
{ _persistedSettingTypes = value; }
}
protected
virtual
GridSettingsCollection Settings
{
get
{
return
_settings; }
set
{ _settings = value; }
}
/// <summary>
/// Saves the current grid settings and returns the settings serilized to string
/// </summary>
public
virtual
string
SaveSettings()
{
return
GetSavedSettings().ToString();
}
/// <summary>
/// Saves the current grid settings and retrieves the underlying
/// GridSettingsCollection instance that contains the grid settings
/// </summary>
public
virtual
GridSettingsCollection GetSavedSettings()
{
if
(Grid ==
null
)
{
throw
new
NullReferenceException();
}
if
(IsSettingSpecified(GridSettingsType.Paging)) SavePagingSettings();
if
(IsSettingSpecified(GridSettingsType.Grouping)) SaveGroupByExpressions();
if
(IsSettingSpecified(GridSettingsType.Sorting)) SaveSortExpressions();
if
(IsSettingSpecified(GridSettingsType.Filtering)) SaveFilterExpression();
if
(IsSettingSpecified(GridSettingsType.ColumnSettings)) SaveColumnSettings();
if
(IsSettingSpecified(GridSettingsType.AllowFiltering)) SaveAllowFiltering();
return
Settings;
}
protected
bool
IsSettingSpecified(GridSettingsType settingType)
{
return
(PersistedSettingTypes & GridSettingsType.All) == GridSettingsType.All ||
(PersistedSettingTypes & settingType) == settingType;
}
protected
virtual
void
SavePagingSettings()
{
Settings.PageSize = Grid.MasterTableView.PageSize;
Settings.PageIndex = Grid.MasterTableView.CurrentPageIndex;
}
protected
virtual
void
SaveGroupByExpressions()
{
Settings.GroupByExpressionsStates =
new
object
[Grid.MasterTableView.GroupByExpressions.Count];
for
(
int
i = 0; i < Settings.GroupByExpressionsStates.Length; i++)
{
Settings.GroupByExpressionsStates[i] =
((IStateManager) Grid.MasterTableView.GroupByExpressions[i]).SaveViewState();
}
}
protected
virtual
void
SaveSortExpressions()
{
Settings.SortExpressionsState = ((IStateManager) Grid.MasterTableView.SortExpressions).SaveViewState();
}
protected
virtual
void
SaveFilterExpression()
{
Settings.FilterExpression = Grid.MasterTableView.FilterExpression;
}
protected
virtual
void
SaveAllowFiltering()
{
Settings.AllowFiltering = Grid.MasterTableView.AllowFilteringByColumn;
}
protected
virtual
void
SaveColumnSettings()
{
Settings.ColumnSettings.Clear();
foreach
(GridColumn column
in
Grid.MasterTableView.Columns)
{
Settings.ColumnSettings.Add(GetColumnSettings(column));
}
Settings.AutoGeneratedColumnSettings.Clear();
foreach
(GridColumn column
in
Grid.MasterTableView.AutoGeneratedColumns)
{
Settings.AutoGeneratedColumnSettings.Add(GetColumnSettings(column));
}
}
private
ColumnSettings GetColumnSettings(GridColumn column)
{
ColumnSettings colSettings =
new
ColumnSettings();
colSettings.UniqueName = column.UniqueName;
colSettings.Width = column.HeaderStyle.Width;
colSettings.Visible = column.Visible;
colSettings.Display = column.Display;
colSettings.OrderIndex = column.OrderIndex;
colSettings.CurrentFilterFunction = column.CurrentFilterFunction;
colSettings.CurrentFilterValue = column.CurrentFilterValue;
return
colSettings;
}
private
void
SetColumnSettings(
ref
GridColumn column, ColumnSettings setting)
{
column.Display = setting.Display;
column.Visible = setting.Visible;
column.HeaderStyle.Width = setting.Width;
column.OrderIndex = setting.OrderIndex;
column.CurrentFilterFunction = setting.CurrentFilterFunction;
column.CurrentFilterValue = setting.CurrentFilterValue;
}
/// <summary>
/// Loads grids settings from a serialized string
/// </summary>
/// <param name="value">The string that contains the serialized settings</param>
public
virtual
void
LoadSettings(
string
value)
{
LoadSettings(GridSettingsCollection.LoadFromSerializedData(value));
}
/// <summary>
/// Loads grids settings from a byte array
/// </summary>
/// <param name="data">The byte array that contains the serialized grid settings</param>
public
virtual
void
LoadSettings(
byte
[] data)
{
LoadSettings(GridSettingsCollection.LoadFromSerializedData(data));
}
/// <summary>
/// Loads grid settings from a GridSettingsCollection instance
/// </summary>
/// <param name="settings">The GridSettingsCollection instance to load settings from</param>
public
virtual
void
LoadSettings(GridSettingsCollection settings)
{
if
(Grid ==
null
|| settings ==
null
)
{
throw
new
NullReferenceException();
}
Settings = settings;
if
(IsSettingSpecified(GridSettingsType.Paging)) LoadPagingSettings();
if
(IsSettingSpecified(GridSettingsType.Grouping)) LoadGroupByExpressions();
if
(IsSettingSpecified(GridSettingsType.Sorting)) LoadSortExpressions();
if
(IsSettingSpecified(GridSettingsType.Filtering)) LoadFilterExpression();
if
(IsSettingSpecified(GridSettingsType.ColumnSettings)) LoadColumnSettings();
if
(IsSettingSpecified(GridSettingsType.AllowFiltering)) LoadAllowFiltering();
}
protected
virtual
void
LoadPagingSettings()
{
if
(Grid.MasterTableView.AllowPaging && Settings.PageSize > 0)
{
Grid.MasterTableView.PageSize = Settings.PageSize;
Grid.MasterTableView.CurrentPageIndex = Settings.PageIndex;
}
}
protected
virtual
void
LoadGroupByExpressions()
{
if
(Settings.GroupByExpressionsStates ==
null
)
return
;
Grid.MasterTableView.GroupByExpressions.Clear();
foreach
(
object
expressionState
in
Settings.GroupByExpressionsStates)
{
GridGroupByExpression expression =
new
GridGroupByExpression();
((IStateManager) expression).LoadViewState(expressionState);
Grid.MasterTableView.GroupByExpressions.Add(expression);
}
}
protected
virtual
void
LoadSortExpressions()
{
if
(Settings.SortExpressionsState ==
null
)
return
;
((IStateManager) Grid.MasterTableView.SortExpressions).LoadViewState(Settings.SortExpressionsState);
}
protected
virtual
void
LoadFilterExpression()
{
Grid.MasterTableView.FilterExpression = Settings.FilterExpression;
}
protected
virtual
void
LoadAllowFiltering()
{
Grid.MasterTableView.AllowFilteringByColumn = Settings.AllowFiltering;
}
protected
virtual
void
LoadColumnSettings()
{
if
(Settings.AutoGeneratedColumnSettings.Count > 0)
{
Grid.ColumnCreated +=
new
GridColumnCreatedEventHandler(Grid_ColumnCreated);
}
foreach
(ColumnSettings colSetting
in
Settings.ColumnSettings)
{
GridColumn column = Grid.MasterTableView.GetColumnSafe(colSetting.UniqueName);
if
(column !=
null
)
{
SetColumnSettings(
ref
column, colSetting);
}
}
}
private
void
Grid_ColumnCreated(
object
sender, GridColumnCreatedEventArgs e)
{
ColumnSettings settings = Settings.AutoGeneratedColumnSettings.Find(
delegate
(ColumnSettings
set
)
{
return
set
.UniqueName ==
e.Column.UniqueName;
});
GridColumn column = e.Column;
if
(settings !=
null
)
{
SetColumnSettings(
ref
column, settings);
}
}
}
/// <summary>
/// Enumerates the types of grid settings that can be persisted
/// </summary>
[Flags]
public
enum
GridSettingsType
{
Paging = 1,
Sorting = 2,
Filtering = 4,
Grouping = 8,
ColumnSettings = 16,
AllowFiltering = 32,
All = 64
}
/// <summary>
/// Represents a collection of grid settings
/// </summary>
[Serializable]
public
class
GridSettingsCollection
{
private
int
_pageSize;
private
int
_pageIndex;
private
object
[] _groupByExpressionsStates;
private
object
_sortExpressionsState;
private
string
_filterExpression;
private
List<ColumnSettings> _columnSettings;
private
List<ColumnSettings> _autoColumnSettings;
private
bool
_allowFiltering;
public
int
PageSize
{
get
{
return
_pageSize; }
set
{ _pageSize = value; }
}
public
int
PageIndex
{
get
{
return
_pageIndex; }
set
{ _pageIndex = value; }
}
public
object
[] GroupByExpressionsStates
{
get
{
return
_groupByExpressionsStates; }
set
{ _groupByExpressionsStates = value; }
}
public
object
SortExpressionsState
{
get
{
return
_sortExpressionsState; }
set
{ _sortExpressionsState = value; }
}
public
string
FilterExpression
{
get
{
return
_filterExpression; }
set
{ _filterExpression = value; }
}
public
List<ColumnSettings> ColumnSettings
{
get
{
return
_columnSettings; }
set
{ _columnSettings = value; }
}
public
List<ColumnSettings> AutoGeneratedColumnSettings
{
get
{
return
_autoColumnSettings; }
set
{ _autoColumnSettings = value; }
}
public
bool
AllowFiltering
{
get
{
return
_allowFiltering; }
set
{ _allowFiltering = value; }
}
/// <summary>
/// Returns the serialized object as string
/// </summary>
public
override
string
ToString()
{
LosFormatter formatter =
new
LosFormatter();
using
(StringWriter writer =
new
StringWriter())
{
formatter.Serialize(writer,
this
);
return
writer.ToString();
}
}
/// <summary>
/// Returns the serialized object as byte array
/// </summary>
public
byte
[] ToArray()
{
LosFormatter formatter =
new
LosFormatter();
using
(MemoryStream stream =
new
MemoryStream())
{
formatter.Serialize(stream,
this
);
return
stream.ToArray();
}
}
/// <summary>
/// Gets the GridSettingsCollectionInstance from its serialized string data
/// </summary>
/// <param name="data">The object as serialized string data</param>
public
static
GridSettingsCollection LoadFromSerializedData(
string
data)
{
LosFormatter formatter =
new
LosFormatter();
return
(GridSettingsCollection) formatter.Deserialize(data);
}
/// <summary>
/// Gets the GridSettingsCollectionInstance from its serialized byte array
/// </summary>
/// <param name="data">The object as serialized byte array</param>
public
static
GridSettingsCollection LoadFromSerializedData(
byte
[] data)
{
LosFormatter formatter =
new
LosFormatter();
using
(MemoryStream stream =
new
MemoryStream(data))
{
return
(GridSettingsCollection) formatter.Deserialize(stream);
}
}
}
/// <summary>
/// Represents a collection of grid column settings
/// </summary>
[Serializable]
public
class
ColumnSettings
{
private
string
_uniqueName;
private
int
_orderIndex;
private
Unit _width;
private
bool
_visible;
private
bool
_display;
private
GridKnownFunction _currentFilterFunction;
private
string
_currentFilterValue;
public
string
UniqueName
{
get
{
return
_uniqueName; }
set
{ _uniqueName = value; }
}
public
int
OrderIndex
{
get
{
return
_orderIndex; }
set
{ _orderIndex = value; }
}
public
Unit Width
{
get
{
return
_width; }
set
{ _width = value; }
}
public
bool
Visible
{
get
{
return
_visible; }
set
{ _visible = value; }
}
public
bool
Display
{
get
{
return
_display; }
set
{ _display = value; }
}
public
GridKnownFunction CurrentFilterFunction
{
get
{
return
_currentFilterFunction; }
set
{ _currentFilterFunction = value; }
}
public
string
CurrentFilterValue
{
get
{
return
_currentFilterValue; }
set
{ _currentFilterValue = value; }
}
}
}
0

Ryan
Top achievements
Rank 1
answered on 12 Dec 2011, 08:06 PM
This still does not work..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
SetGridSettings()
End If
End Sub
Protected Sub SetGridSettings()
Dim SavePersister As New GridSettingsPersister(RadGrid1)
Session("qa_home_filter") = SavePersister.SaveSettings()
Dim LoadPersister As New GridSettingsPersister(RadGrid1)
Dim settings As String = DirectCast(Session("qa_home_filter"), String)
LoadPersister.LoadSettings(settings)
End Sub
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles RadGrid1.PreRender
Dim SavePersister As New GridSettingsPersister(RadGrid1)
Session("qa_home_filter") = SavePersister.SaveSettings()
End Sub
0

Ryan
Top achievements
Rank 1
answered on 12 Dec 2011, 09:01 PM
This works....
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
SetGridSettings()
End If
End Sub
Protected Sub SetGridSettings()
Dim LoadPersister As New GridSettingsPersister(RadGrid1)
If Session("qa_home_filter") = Nothing Then
Else
Dim settings As String = DirectCast(Session("qa_home_filter"), String)
LoadPersister.LoadSettings(settings)
End If
End Sub
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles RadGrid1.PreRender
Dim SavePersister As New GridSettingsPersister(RadGrid1)
Session("qa_home_filter") = SavePersister.SaveSettings()
End Sub