We are trying to reduce the grid ViewState using a PageStatePersister and database.
Everything is working fine also using the advanced data-binding except for the exporting functionality (xls, pdf or csv) does not matter the format.
The exported file is correct, we ignore the paging and the whole data is saved correctly, but if we click for example on the pager, the RadGrid loses the paging and also the excel button on the command items, this happens only if we have already exported a file.
The same code without the PageStatePersister works fine.
We can't figure out where the error is, do you have any clues?
Everything is working fine also using the advanced data-binding except for the exporting functionality (xls, pdf or csv) does not matter the format.
The exported file is correct, we ignore the paging and the whole data is saved correctly, but if we click for example on the pager, the RadGrid loses the paging and also the excel button on the command items, this happens only if we have already exported a file.
The same code without the PageStatePersister works fine.
We can't figure out where the error is, do you have any clues?
public
class
SqlPageStatePersister : PageStatePersister
{
private
const
string
VSKEY =
"__VSKEY"
;
private
const
string
VSPREFIX =
"VIEWSTATE_"
;
private
static
readonly
PageViewStateRepository Repository =
new
PageViewStateRepository();
public
SqlPageStatePersister(Page page)
:
base
(page)
{
}
public
override
void
Load()
{
if
(!Page.IsPostBack)
return
;
string
vsKey = Page.Request.Form[VSKEY];
// Sanity Checks
if
(
string
.IsNullOrEmpty(vsKey))
throw
new
ViewStateException();
if
(!vsKey.StartsWith(VSPREFIX))
throw
new
ViewStateException();
IStateFormatter frmt = StateFormatter;
string
state = GetViewState(vsKey);
if
(
string
.IsNullOrEmpty(state))
return
;
var statePair = frmt.Deserialize(state)
as
Pair;
if
(statePair ==
null
)
return
;
ViewState = statePair.First;
ControlState = statePair.Second;
}
public
override
void
Save()
{
if
(Page.Session ==
null
)
throw
new
InvalidOperationException(
"Session is required for SqlPageStatePersister (SessionID -> Key)"
);
if
(ViewState !=
null
|| ControlState !=
null
)
{
string
vsKey;
if
(!Page.IsPostBack)
{
string
sessionId = Page.Session.SessionID;
string
pageUrl = Page.Request.Path;
vsKey =
string
.Format(
"{0}{1}_{2}_{3}"
, VSPREFIX, pageUrl, sessionId, DateTime.Now.Ticks);
}
else
{
vsKey = Page.Request.Form[VSKEY];
if
(
string
.IsNullOrEmpty(vsKey))
throw
new
ViewStateException();
}
IStateFormatter frmt = StateFormatter;
string
state = frmt.Serialize(
new
Pair(ViewState, ControlState));
StoreViewState(vsKey, state);
Page.Cache.Add(vsKey, vsKey,
null
, DateTime.Now.AddMinutes(Page.Session.Timeout),
Cache.NoSlidingExpiration, CacheItemPriority.Low, ViewStateCacheRemoveCallback);
Page.ClientScript.RegisterHiddenField(VSKEY, vsKey);
}
}
#region Database
private
static
void
StoreViewState(
string
key,
string
viewStateData)
{
var item =
new
PageViewState {ID = key, Value = viewStateData, LastUpdatedOn = DateTime.Now};
Repository.InsertOrUpdate(item);
}
private
static
string
GetViewState(
string
key)
{
PageViewState vs = Repository.Select(key);
if
(vs !=
null
)
return
vs.Value;
return
string
.Empty;
}
private
static
void
ViewStateCacheRemoveCallback(
string
key,
object
value, CacheItemRemovedReason reason)
{
Repository.Delete(Repository.Select(key));
}
#endregion
}
}
public
class
SqlPageAdapter : PageAdapter
{
public
override
PageStatePersister GetStatePersister()
{
return
new
SqlPageStatePersister(Page);
}
}
<
browsers
>
<
browser
refID
=
"Default"
>
<
controlAdapters
>
<
adapter
controlType
=
"System.Web.UI.Page"
adapterType
=
"DataValidator.PageState.SqlPageAdapter"
/>
</
controlAdapters
>
</
browser
>
</
browsers
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AllowFilteringByColumn
=
"True"
AllowPaging
=
"True"
CellSpacing
=
"0"
GridLines
=
"None"
OnNeedDataSource
=
"NeedDataSource"
AllowSorting
=
"True"
AutoGenerateColumns
=
"False"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
EnableViewState
=
"True"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
MasterTableView
DataKeyNames
=
"ID"
PageSize
=
"20"
CommandItemDisplay
=
"Top"
EnableViewState
=
"True"
>
<
CommandItemSettings
ShowAddNewRecordButton
=
"false"
ShowExportToExcelButton
=
"true"
>
</
CommandItemSettings
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Version"
HeaderText
=
"Version"
SortExpression
=
"Version"
UniqueName
=
"Version"
FilterControlWidth
=
"95px"
/>
<
telerik:GridDateTimeColumn
DataField
=
"Date"
HeaderText
=
"Date"
SortExpression
=
"Date"
UniqueName
=
"Date"
FilterControlWidth
=
"95px"
PickerType
=
"DatePicker"
EnableTimeIndependentFiltering
=
"true"
/>
<
telerik:GridBoundColumn
DataField
=
"Level"
HeaderText
=
"Level"
SortExpression
=
"Level"
UniqueName
=
"Level"
FilterControlWidth
=
"95px"
/>
<
telerik:GridBoundColumn
DataField
=
"Message"
HeaderText
=
"Type"
SortExpression
=
"Message"
UniqueName
=
"Message"
FilterControlWidth
=
"95px"
/>
<
telerik:GridBoundColumn
DataField
=
"Exception"
HeaderText
=
"Message"
SortExpression
=
"Exception"
UniqueName
=
"Exception"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"UserName"
HeaderText
=
"UserName"
SortExpression
=
"UserName"
UniqueName
=
"UserName"
FilterControlWidth
=
"95px"
/>
<
telerik:GridBoundColumn
DataField
=
"Page"
HeaderText
=
"Page"
SortExpression
=
"Page"
UniqueName
=
"Page"
FilterControlWidth
=
"95px"
/>
</
Columns
>
</
MasterTableView
>
<
ExportSettings
ExportOnlyData
=
"true"
IgnorePaging
=
"true"
OpenInNewWindow
=
"true"
>
<
Excel
Format
=
"Biff"
></
Excel
>
</
ExportSettings
>
<
FilterMenu
EnableImageSprites
=
"False"
>
</
FilterMenu
>
<
PagerStyle
Mode
=
"NumericPages"
></
PagerStyle
>
</
telerik:RadGrid
>
namespace
DataValidator._uc
{
public
partial
class
LogCtrl : UserControlHelper, IGridUserControl
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
#region Implementation of IGridUserControl
public
void
NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
var lr =
new
LogRepository();
RadGrid1.DataSource = lr.Select();
}
public
void
UpdateCommand(
object
sender, GridCommandEventArgs e)
{
}
public
void
DeleteCommand(
object
sender, GridCommandEventArgs e)
{
}
public
void
InsertCommand(
object
sender, GridCommandEventArgs e)
{
}
#endregion
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
var log = (Log) e.Item.DataItem;
if
(log !=
null
)
{
string
message = log.Exception;
if
(message.Length > 100)
message =
string
.Format(
"{0}..."
, message.Substring(0, 46));
e.Item.Cells[6].Wrap =
false
;
e.Item.Cells[6].ToolTip = log.Exception;
e.Item.Cells[6].Text = message;
}
}
}
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.ExportToExcelCommandName)
{
PageHelper.Exporting =
true
;
}
}
}
}