C# -state

I found some good info about ViewState at http://www.extremeexperts.com/Net/Articles/ViewState.aspx.  Below is an example (from above site) showing scenario where ViewState should be disabled for the textBox on the webform.

Can you please provide two or three other exampes showing that ViewState can be disabled for specific control on webform..and why it needs to be disabled ???

After loading the view state, the page reads client-side information through the Request object and uses those values to override most of the settings for the server controls. In general, the two operations are neatly separated and take place independently. In particular, though, the second operation—reading from Request.Form—in many situations ends up just overriding the settings read out of the view state. In this particular case the view state is only an extra overhead. For example consider the following case, we have one textbox in the page and a link button. If you are typing the some  values in to the textbox and the posting the page using linkbutton. After postback, value in the textbox is retained though you enable or disable the viewstate. In this case you shouldnt enable viewstate for this textbox. Viewstate value is overridden by request.form values, since loadpostdata fires after loadviewstate view event in the Page lifecycle.

Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Any time you have programmatic changes to the state of one or more of your controls, those affected controls are the ones for which you would want to use ViewState. If you programmatically change the text of the button on a PostBack, then you would probably want that in ViewState. If the controls receive their text from the markup only (i.e. the text is static), then there is no need to add to the ViewState--the state for those particular controls is automatically restored by virtue of the instructions in the markup.
käµfm³d 👽Commented:
Can you please provide two or three other exampes showing that ViewState can be disabled for specific control on webform
It's the same for all controls, but:

<asp:TextBox ID="TextBox1" runat="server" EnableViewState="false"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="false"></asp:Label>
<asp:Table ID="Table1" runat="server" EnableViewState="false"></asp:Table>
<asp:Button ID="Button1" runat="server" Text="Button" EnableViewState="false" />

Open in new window

why it needs to be disabled
Read "The Role of ViewState" in this link: http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic9
niceguy971Author Commented:

1)  For  the Button control (when we do not change Text property ) it ALWAYS makes sense to Disable ViewState correct???

<asp:Button ID="Button1" runat="server" Text="Continue" EnableViewState="false" />

2) For the Label Control (which we use to Display the same text ) it ALWAYS makes sense to Disable ViewState correct???

<asp:Label ID="Label1" runat="server" Text="Please enter your Name" EnableViewState="false"></asp:Label>

niceguy971Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.