How can I decide what to store in viewstate?

I am writing a bunch of common ASP.NET controls, and one thing that I can’t plunge into is when to store values ​​in the viewstate and when to assume that this is normal, not.

On the one hand, it makes sense to store the entire state of the control in the viewstate, including properties such as:

  • Text field values ​​entered by the user (or data of any form)
  • Configuration options such as height or page size
  • Even how the control is configured - for example, to store all the data of which the grid is built, or the grid itself.

Ignoring performance, the more you can get hung up on viewstate, the better, because it means that the control will behave the same way through callbacks and will never “accidentally” return a value or “forget” to turn it off. But viewstate is not free. Saving all means that now the control will output both HTML and all its internal properties to create this HTML, which will almost always more than double the result.

My question is not about performance, but about strategy. On what criteria did I decide to place the property in the viewstate? I thought something in these lines:

If the user cannot change the property, then the server will always set it explicitly, so he should leave it outside the viewing field. Even for something like the color=reduser does not set this property directly; they will press a button elsewhere that indirectly sets this property. This button or its owner should maintain state, not a control that makes the color red.

This logic implies that the only properties that should be included in the viewstate will be the following:

  • Form elements such as <input>(and Request.Form[c.UniqueID]this can be avoided)
  • Properties that the user can control interactively directly on the control.

Does this logic make sense? This seems weak, and I would like to hear more from experts.

+5
3

ViewState , .

ControlState , , ViewState .

( html- ) ASP.NET, . , ( ViewState ).

ViewState, ( TrackViewState). , , "". , TextBox1.Text page_load, ViewState.IsItemDirty("TextBox1.Text") true. ViewState.

. ( )

+4

MSDN, , , ASP.NET, - :

: http://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx

Viewstate Excerpt:

- ViewState . . . ASP.NET.

. , , , , , - .

:

. .

- . .

, Unicode, , .

:

, , . , .

.

. , . , , . . ASP.NET - .

+3

I think you are right to worry about inflating viewstate, but what other options are available to you? If you do not store variable data where you put it? (You might want to remove some configuration items - you might not allow the user to change so many properties).

0
source

All Articles