hello experts,
I have a weired problem regarding viewstate comression.
I have implemented override methods LoadPageStateFromPersistenceMedium() and SavePageStateToPersistenceMedium(object viewState).
Everything works fine. But the problem is when I hit Escape button twice in any textbox, the viewstate gets cleared and following error occures whle decompressing the viewstate in LoadPageStateFromPersistenceMedium.
---------------------------
Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?
Line: 5
Error: Sys.WebForms.PageRequestManagerServerErrorException: Value cannot be null.Parameter name: inputString
---------------------------
Yes No
---------------------------
My original code is:
protected override object LoadPageStateFromPersistenceMedium()
{
string viewState = Request.Form["__VSTATE"]; //This remains empty strinf after hitting escape twice.
byte[] bytes = Convert.FromBase64String(viewState);
bytes = ViewStateCompressor.DecompressViewState(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = ViewStateCompressor.CompressViewState(bytes);
ScriptManager.RegisterHiddenField(this, "__VSTATE", Convert.ToBase64String(bytes));
}
I tried some workarounds in above code.
WORKAROUND 1:
In the LoadPageStateFromPersistenceMedium() method, after 1st statement i.e. string viewState = Request.Form["__VSTATE"]; I wrote following code:
if (viewState.IsNotNullOrNotEmpty(true))
Session["VSTATE"] = viewState;
else
viewState = Session["VSTATE"].ConvertToString();
So that, I can always make sure that, if viewstate is empty then take it from session which i have already saved. But this approch didn't work.
WORKAROUND 2:
This time I tried to avoid save viewstate in hiddenfields, instead i tried to save viewstate in sessions. For this I made following chnages in above 2 mehods. Commented original code, and written new code just below commented code.
protected override object LoadPageStateFromPersistenceMedium()
{
//string viewState = Request.Form["__VSTATE"];
//byte[] bytes = Convert.FromBase64String(viewState);
byte[] bytes = Convert.FromBase64String(Session["VSTATE"].ConvertToString());
bytes = ViewStateCompressor.DecompressViewState(bytes);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(Convert.ToBase64String(bytes));
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = ViewStateCompressor.CompressViewState(bytes);
//ScriptManager.RegisterHiddenField(this, "__VSTATE", Convert.ToBase64String(bytes));
Session["VSTATE"] = Convert.ToBase64String(bytes);
}
But this approch also failed. Both workaround gives me following error.
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Please help, as my application has some scenarios where user needs to hit escape in the textboxes. If user hits it twice then I am dead. :(
-
Harshal