Compression issues in ViewState

I'm fighting the .NET Viewstate right now and it is starting to lower me. Having discovered that some of the pages in one of our applications consist of approximately 80% of the views, I studied this when I can.

I looked through (and am pleased) disabling the viewstate for controls that it doesn't need (shortcuts, buttons, etc.), and made some small gains here.

Now I am looking at compression in the view, and although I can demonstrate a 40-50% reduction, it does not seem to work very well with my application.

Scenario: The page contains several drop-down lists, a button and a Grdiview (therefore, you must deal with ViewState!). When the page loads, DDL are populated and selected by default. Clicking OK will populate the Gridview as expected.

Now the problem: if the inclusion of ViewState Compression is enabled, if the user changes the selected elements in the DDL before clicking the “OK” button, they will receive a “Required Field Validator” error indicating that the selection was not made in one of the DDL - But this is not so ! Disabling the compression code fixes the problem and the page works as expected (i.e. for several months!).

Can the problem remain in the view, which is now stored in a key other than __VIEWSTATE [the code that I saw uses different key names - for example, VSTATE).

The stories of my page look like this:

Compressed page source (note the __VIEWSTATE empty key):

<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VSTATE" id="__VSTATE" value="H4sIAAAAAEAO29B2AcSZYlJ
.
.
MKd2afqdaImFR5UiFXVyQPwLPA//8xt+pMsSQ8vlOklcoNgmZfJd8hHvk6/S/7UbxxAJTjzZfp6Qcm039
h3d3dvvPO7/Oa/7i57uemj1H2a/gw5lJQ+ySjFRtPZUL7A/3o2ImFR5UiFXVyLPA+38At70F1EkwAAA=" />
<input type="hidden" name="__VIEWSTATE" id="
__VIEWSTATE" value="" />
</div>

Page source without compression:

<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTYxOTM1NDg4N
A9kFgJmD2QWAgIDD2QWAgIFD2QWAmYPZBYKAgEPZBYIAgcPZBYCAgMPDxYCHgRUZXh0BRdEYXduQyBbY2hhbm
dlIHBhc3N3b3JkXWRkAgkPFgIeB1Zpc2libGVoFgQCAQ8PFgIfAAUFQWRtaW5kZAIDDw8WAh8ABQUxNDoyNGR
.
.
.
.
.
.
kAgsPDxYEHwAFWVNlbGVjdGVkIFNlcnZpY2UgVXNlcjogPGEgY2xhc3M9J3N1U2VsZWN0b3InIGhyZWY9J2xp
c3RzZXJ2aWNldXNlcnMuYXNweCc+PGI+bm9uZTwvYj48L2E+HwFoZGQCDw8QZGQWAGQCBQ8UKwADZDwrABQEA
BYSHg9QYXJlbnRJdGVtQ2xhc3MFC2lnbW5fUGFyZW50HhdUb3BMZXZlbFBhcmVudEl0ZW1DbGFzcwUTaWdtbl
Ub3BFBhcmVudB4KSlNGaWxlTmFtZWUeFlRvcExldmVsSG92ZXJJdGVtQ2xhc3MFNGlnbW5fVG9wTGV"     
/>
</div>

How does .NET know where VIEWSTATE is stored, and does it know that I moved it?

- SavePageStateToPersistenceMedium LoadPageStateFromPersistenceMedium?

+5
6

hey viewstate compression . aspx, viewstate. viewstate, viewstate. 2 :

1) - 2) - viewstate

, . . ,

using System;
using System.IO;
using System.Web.UI;

namespace XC.UI.WebForms
{

    public class PageBase : System.Web.UI.Page
    {

        protected override object LoadPageStateFromPersistenceMedium()
        {
            string viewState = Request.Form["__VSTATE"];
            byte[] bytes = Convert.FromBase64String(viewState);
            bytes = XC.Common.ViewStateHelper.Decompress(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 = XC.Common.ViewStateHelper.Compress(bytes);
            ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
        }

    }

}
+3

ajax , , . . ClientScript.RegisterHiddenField( "__ VSTATE", Convert.ToBase64String ()); ScriptManager. ScriptManager.RegisterHiddenField(, "__VSTATE", Convert.ToBase64String ());

+3

.NET , viewstate. , , .

+2

, www.strangeloop.net. , .

tho - viewstate? ? , , " " , td, tr, div .., runat = "". viewstate. 40% , , div runat = "server".

+1

, OP ( ). -.


- , , ! - ClientScript.RegisterHiddenField . , .. Base.SavePageStateToPersistenceMedium( ) Pair, . LoadPageStateFromPersistenceMedium() . 70% , .

Now that compression works for me, I need to recommend that the next (first correct) code review focus on removing the viewstate where it is not needed.

0
source

All Articles