Hi,
In my current project, I have to write a web page containing a RadGrid with Persisted Sorting.
In my current project, I have to write a web page containing a RadGrid with Persisted Sorting.
I have implement the web page similar to as follows RadGrid with Persisted Sorting
The RadGrid's SortExpressions are persisted in the Settings which are saved (i.e. serialized) on Page_UnLoad and loaded (i.e. deserialized) on Page_Load.
I created the class SortExpressions instead of using Telerik's GridSortExpression because GridSortExpression is not marked as Serializable.
The web page works as expected. However, can you please review my code if I have implemented it correctly?
The code is given as follows:
ASPX
ASPX
<form id="form1" runat="server"> |
<div> |
<telerik:RadScriptManager ID="radScriptManager" runat="server" /> |
<telerik:RadGrid ID="radGrid" AutoGenerateColumns="false" AllowSorting="true" OnNeedDataSource="radGrid_NeedDataSource" runat="server"> |
<ClientSettings> |
<Scrolling UseStaticHeaders="true" /> |
</ClientSettings> |
<MasterTableView AllowMultiColumnSorting="true" TableLayout="Fixed"> |
<Columns> |
<telerik:GridBoundColumn DataField="Name" DataType="System.String" HeaderText="Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="Name" AllowSorting="true" UniqueName="Name" /> |
<telerik:GridBoundColumn DataField="Founder" DataType="System.String" HeaderText="Founder" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="Founder" AllowSorting="true" UniqueName="Founder" /> |
</Columns> |
</MasterTableView> |
</telerik:RadGrid> |
</div> |
</form> |
C#
public partial class RadGridWithSorting : Page |
{ |
private const string SettingsFilename = "Settings.bin"; |
private Settings _settings = null; |
private List<Company> _companies = null; |
protected Settings Settings |
{ |
get |
{ |
if (this._settings == null) |
{ |
this._settings = new Settings(); |
} |
return this._settings; |
} |
set |
{ |
this._settings = value; |
} |
} |
protected void Page_Load(object sender, EventArgs e) |
{ |
LoadSettings(); |
LoadSortExpressions(); |
if (!IsPostBack) |
{ |
this._companies = Company.Load(); |
Session["COMPANIES"] = this._companies; |
} |
else |
{ |
this._companies = (List<Company>)Session["COMPANIES"]; |
} |
} |
protected void Page_UnLoad(object sender, EventArgs e) |
{ |
SaveSortExpressions(); |
SaveSettings(); |
} |
protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) |
{ |
this.radGrid.DataSource = this._companies; |
} |
private void LoadSettings() |
{ |
Settings = Helper.Load(SettingsFilename); |
} |
private void LoadSortExpressions() |
{ |
this.radGrid.MasterTableView.SortExpressions.Clear(); |
foreach (SortExpression sortExpression in Settings.SortExpressions) |
{ |
GridSortExpression gridSortExpression = new GridSortExpression(); |
gridSortExpression.FieldName = sortExpression.FieldName; |
gridSortExpression.SortOrder = sortExpression.SortOrder; |
this.radGrid.MasterTableView.SortExpressions.AddSortExpression(gridSortExpression); |
} |
} |
private void SaveSettings() |
{ |
Helper.Save(Settings, SettingsFilename); |
} |
private void SaveSortExpressions() |
{ |
Settings.SortExpressions.Clear(); |
foreach (GridSortExpression sortExpression in this.radGrid.MasterTableView.SortExpressions) |
{ |
Settings.SortExpressions.Add(new SortExpression(sortExpression.FieldName, sortExpression.SortOrder)); |
} |
} |
} |
[Serializable] |
public class Settings |
{ |
public List<SortExpression> SortExpressions { get; set; } |
public Settings() |
{ |
SortExpressions = new List<SortExpression> {}; |
} |
public Settings(List<SortExpression> sortExpressions) |
{ |
SortExpressions = sortExpressions; |
} |
} |
[Serializable] |
public class SortExpression |
{ |
public string FieldName { get; set; } |
public GridSortOrder SortOrder { get; set; } |
public SortExpression() |
{ |
FieldName = null; |
SortOrder = GridSortOrder.None; |
} |
public SortExpression(string fieldName, GridSortOrder sortOrder) |
{ |
FieldName = fieldName; |
SortOrder = sortOrder; |
} |
} |
Thanks,
Herman