Reputation: 5862
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?
Upvotes: 1
Views: 1661
Reputation: 4764
Render the html using the jquery ajax so that there is no problem of viewstate. This will give performance and put less load in the server.
Upvotes: 0
Reputation: 170
It depends, I generally do not worry too much about the view state for controls such as dropdown etc (provided it is not populated with 100s of items, which will make it unusable anyway).
For controls like grid, I would turn off the view state, specially telerik (which has nice alternatives).
You can use caching to save your database trips by the way.
Upvotes: 0
Reputation: 44605
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.
Upvotes: 2
Reputation: 47530
I don't think ViewState can be a big burden for around 20 controls. It's much easier for you to do as the first attempt and do improvements later if necessary. However that depends on the data that uses in your application.
I find below interesting.
Improve Performance By Compressing ViewState
Upvotes: 1
Reputation: 31077
Generally, do use viewstate unless you have a lot of data. It depends on the application and the situation.
My suggestion is that you use it now, and optimize things later. You can even disable viewstate for specific controls which carry lots of data. It seems you do not have a good enough reason not to use it right now.
Upvotes: 1