6

For some reason the viewstate of my application became gigantic (around 14 million characters). It adds around 1 minute of loading time. If the page finally loads (which is not often), the server crashes every time someone send a form because "Post size exceeded allowed limits. "

It appeared suddenly. I didn't add any fields, just some javascript on the page.

People told me to check viewstate chunking out. Google told me to do this:

<pages maxPageStateFieldLength="1024">

... so now instead of a huge hidden field I now have something like 100 very large hidden fields. It's not exactly what I was looking for.

Why would .NET do something like this? How can I fix this?

marcgg
  • 65,020
  • 52
  • 178
  • 231
  • Any chance you can turn off viewstate for a few of the controls? – Chris Van Opstal Aug 24 '09 at 17:05
  • 2
    It's hard to say without looking at the page, but try a viewstate decoder to see what's being embedded in the viewstate. You should also turn off viewstate of grids/tables if you don't need them and perhaps implement a compressed viewstate in addition to turning on gzip on IIS. – Mikael Svenson Aug 24 '09 at 17:05
  • The controls are input fields and there is no way to turn off the viewstate for those and even if it was that would require me to re-write a lot of code. Also there are like 10 fields, it shouldn't be that bad... right? – marcgg Aug 24 '09 at 17:06
  • @mikael: how do I turn off viewstate of grids/tables? I only have input fields, is it possible that .net decided that some other fields needed to be in the viewstate? – marcgg Aug 24 '09 at 17:07
  • What are you using the viewstate for on the inputs? If you just need them to remember their values across postbacks, that'll work without viewstate. – Chris Van Opstal Aug 24 '09 at 17:08
  • There's obviously something else going on if you only have 10 fields. Are you using custom controls that use resources? How did you add the javascript to the page? – womp Aug 24 '09 at 17:10
  • @chris: To remember values and use them in my backend as well... I mean I can refactor this, but is it even possible to turn off the viewstate for these? Last time I checked it wasn't working as I would expect – marcgg Aug 24 '09 at 17:11
  • @womp: I generate javascript server side. I've got a couple of loops in the asp and that's pretty much it – marcgg Aug 24 '09 at 17:11
  • @marcgg Textboxes will retain their value across postbacks even without viewstate enabled. – Chris Van Opstal Aug 24 '09 at 17:14
  • @chris: it would be nice to find an answer that doesn't implies that I have to rewrite too much code, and disabling the viewstate for everything would be a bit violent. If there's no other way I'll do it... but not happilly :) – marcgg Aug 24 '09 at 17:19

2 Answers2

7

I would suggest using a utility to decode your viewstate so you can get an idea of what is actually within it (since you obviously have a lot of information in there you don't seem to need.)

A viewstate decoder will allow you to see what's in your viewstate that you aren't expecting. Then you can either modify your code, remove the offending control, or selectively disable viewstate (using the EnableViewState="false" attribute) for the controls that shouldn't have it enabled.

Beska
  • 12,445
  • 14
  • 77
  • 112
  • That's annoying, the software crashes, apparently the string is too large. But now I know that my viewstate is 13 428 604 characters long :) – marcgg Aug 24 '09 at 17:22
  • It finally run without crashing on my 5th attempt :) It looks like it's putting all my page in the viewstate... it's quite weird. I'll try to see what's the logic here and come back on SO – marcgg Aug 24 '09 at 17:27
  • Ok I figured it out. I accept your answer because you're the one that lead me in the right direction... even thought Chris' answer also helped me a lot. The problem was that this page was a search page... and for some reason it included all the results in the viewstate even though there was no search done. I saw that using the viewstate decoder. Then fixing it was simple, I added EnableViewState="false" on the .ascx that kept the search results and it fixed it. yay! – marcgg Aug 24 '09 at 17:31
  • @marcgg That was what I ment by turning off the viewstate :) Glad you figured it out. – Mikael Svenson Aug 28 '09 at 06:37
3

Keep in mind that controls will retain their values across postbacks without viewstate. You can often disable viewstate for a lot of your controls without any issues. To disable viewstate for a specific control set:

EnableViewState="false"

If you set this for all of your grids and any controls that you don't need viewsate for it will significantly reduce the size.

Chris Van Opstal
  • 36,423
  • 9
  • 73
  • 90
  • I did this and it didn't change anything. I'm including .ascx files like this: and there's quite a lot of JS going on in there. Could it be linked? I disabled the viewstate for these and it didn't fix it – marcgg Aug 24 '09 at 17:17
  • I figured it out (see accepted answer)! I accepted beska's because it's the answer that helped me figure out the issue, but yours was also helpful... so +1 and thank you very much :) ! – marcgg Aug 24 '09 at 17:32