ViewStae compression weired error

Last post 07-06-2009 6:53 PM by Nissan Fan. 1 replies.

Sort Posts:

  • ViewStae compression weired error

    07-02-2009, 5:27 AM
    • Member
      52 point Member
    • harshalbhakta
    • Member since 09-13-2006, 12:27 PM
    • MH, India
    • Posts 16

    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

     

    -
    thanks,
    harshal
    Programmme Analyst
  • Re: ViewStae compression weired error

    07-06-2009, 6:53 PM
    Answer
    • Member
      234 point Member
    • Nissan Fan
    • Member since 06-29-2009, 3:49 PM
    • Posts 57

     First of all, compressing the ViewState using this approach is very old school.

    http://www.codeguru.com/vb/vbnet30/article.php/c13931

    Here's a 5 line solution that tucks your ViewState Server Side.  BTW - you'd never actually want to Compress the ViewState as encrypted text like the ViewState doesn't compress very well.

    Regards.

Page 1 of 1 (2 items)