[ACCEPTED]-When should EnableViewState be enabled on a server control?-asp.net

Accepted answer
Score: 21

Here's a good rule of thumb: If you (1) change 28 a property's value in the code-behind, and 27 (2) need to know what value you set in 26 a later postback without recalculating the 25 value, then you need to use ViewState.

For 24 example. In my page's markup I might have 23 a Label control specified like this:

<asp:Label ID="TitleLabel" runat="server" Text="Update this Employee" />

Then 22 in the Page_Load event I have this code:

If Not IsPostBack AndAlso myEmployeeObject.IsNew Then TitleLabel.Text = "Create a new Employee"

By 21 changing the value of the Text property, I've 20 introduced a new element into ViewState. If 19 I get the value of the Label's Text property 18 during any subsequent PostBack, the value 17 will be "Create a new Employee".

Here's 16 what I do when I set out to minimize the 15 amount of ViewState used by my page. I 14 enable tracing on the page. The trace output is added 13 to the bottom of your page when its rendered 12 in the browser. The trace output identifies 11 every single server control on your page 10 and includes how much ViewState (measured 9 in bytes, I believe) each control stores. I 8 use this information to calculate when I 7 want to trade the overhead of ViewState 6 for the overhead of recalculating values.

In 5 my previous example, I would elect to recalculate 4 the Label's Text property on every PostBack 3 and stop storing the Text property in ViewState. This 2 is how my updated markup would look:

<asp:Label ID="TitleLabel" runat="server" Text="Update this Employee" EnableViewState="false" />

And 1 my updated Page_Load event:

If myEmployeeObject.IsNew Then TitleLabel.Text = "Create a new Employee"
Score: 1

Only time you should use viewstate is when 11 you need to get the value of that sucker 10 back on a postback or something. So for 9 the label example you'd only need viewstate 8 enabled if you had code that said something 7 like

void Button1_Click()
{
   label1.text += " more!";
}

without viewstate the postback couldn't 6 figure out the contents of the label and 5 you'd just get " more!" over and over with 4 no append. try it.

Really, our the rule of 3 thumb at my office is just turn it off at 2 the page level and then enable it as you 1 need it.

Score: 1

First understand the view state, here is 21 a blog entry that might help. Start developing 20 your pages by disabling the viewstate at 19 the page level. Most controls in asp .net 18 2.0 save state required for their functioning 17 in the Control State thus disabling view 16 state would not affect most controls.

For 15 controls that do save data bound to them 14 in the view state like List box, you can 13 avoid the data from landing in the view 12 state (which works fine for most use cases) by 11 doing your binding on the PreInit event.

Other 10 than that, if you don't have a third party 9 control that needs it or so, the performance 8 penalty you encur from using the view state 7 far outweighs the promised preservation 6 of state you get between postbacks.

And finally 5 use tools that help you see the bytes going 4 on in your page's view state. The ASP.NET 3 View State helper and an add in into Fiddler 2 which shows viewstate data would help you 1 a lot in this respect.

Score: 0

only enableviewstate when you want to preserve 3 the values across http requests, other than 2 that keep it = false. also you dont have 1 to enableviewstate to use a control.

Score: 0

Whenever you have a control on which the 8 contents will be important (like a text 7 box or drop list) you want to enable viewstate 6 so that the content will available and update 5 to date on a postback.

Anytype of control 4 which outputs somewhat static text (stuff 3 you are not getting back from the user) typically 2 will not have viewstate enable. This minimizes 1 the viewstate.

Score: 0

You need to make sure you understand the 6 ViewState better. No blanket statement 5 like "only enable the ViewState if 4 you have to" will really make sense 3 unless you do. Understand when the viewstate 2 gets loaded/saved/dirtied.

here's one of the better 1 articles i've seen

Score: 0

To be honest I can't think of any time you 3 would want viewstate set to true for label 2 controls. Its a quick way to make your 1 w3wp.exe take up hoards of memory.

More Related questions