I have an webforms website with 20+ controls per page to display data, I have 2 options to persist values:
-
Use web controls to show all my values and take advantage of ViewState. Just need to populate controls when IsNotPostback and they will render same values every time using ViewState.
-
Disable ViewState and populate controls on every postback from database.
I just have to go once to DB, I’m filling the 20 controls using the same query.
Which one is cheaper?, should I use ViewState or not?
based on your input I would say surely is better to have the ViewState overhead back and forth in the transfers rather than executing all the times 20 roundtrips to the database to load the data again and again.
Of course you could have caching and / or query all needed data in a single super fast db call so that could also be cheap in the end, depending on your overall design.
ViewState nowadays should not be a big issue thinking about fast LAN / WAN connections and especially if the web site is used only in the Intranet, then I would leave the ViewState, but I would also try to optimize/minimize db calls in general and cache as well.