Asp.net what to use instead of viewstate

Currently, I use viewstate only to save the current page number I'm on when swapping through data. I have 3 controls on the page that have data that I can skip.

So far, the easiest way to keep track of the page number is with a viewstate, but it is getting really big, and I have no idea why.

So I would like to use something else to keep track of the page number, but I'm not sure what to use. Should I embed it in a hidden form field? Pass it in the query string? Other good options?

+4
source share
4 answers

You declare that you use viewstate to store the current page number, and by this I assume that you explicitly store this number in the viewstate.

However, asp.net will by default store a lot of data in the viewstate. In your example, having 3 controls with paging enabled, asp.net will store "all data in the control", that is, all data that is currently displayed in 3 controls will be stored in viewstate.

The solution to this problem may be to explicitly disable the "turn off" viewstate on the three displayed controls, unfortunately, this means that you will have to double-check the controls on each page, which may or may not be an option for you.

If you just need to save the page number, you can, for example, transfer it to a control state, as described in msdn and pluralsight .

Use the query suggested in another answer.

Or you could just keep using the viewstate and then go on to turn off the viewstate for the whole page or just paged-controls, whatever works for you.

I would really suggest reading the Truly Understanding Viewstate from Mrunal Brahmbhatt for a detailed explanation of the state of the views.

+6
source

Saving page numbers in ViewState should not take up so much space, so I think you should solve the root of your problem.

In ASP.Net, ViewState is enabled by default for each control. So a simple label with static text will take up space in the ViewState. Disabling this will help a lot.

Check out these two articles, they have helped me a lot in the past:

http://www.webreference.com/programming/asp/viewstate/ http://www.webpronews.com/expertarticles/2005/11/07/optimize-aspnet-pages-by-reducing-the-size-of- the-viewstate

What version of ASP.Net are you running? ASP.Net 2.0 is much smaller than ViewState than 1.1.

+2
source

With the apathetic nature of HTTP, it's hard to choose where to actually store your state. In your example, I would consider the following instead of viewstate:

  • Query string - the page number is small enough to fit well into it.
  • Cookies - in this case, your data will be stored longer and will go through all the GET and POST requests.
  • Session - almost similar to the previous option, but the data will be stored on the server.
+2
source

Passing it in the query string is a good option, making it “hacked” and well suited for SEO. Therefore, for the page number, I will never consider anything other than the URL!

+1
source

All Articles