开发者

Understanding Viewstate working procedure

In order to enhance performance of my application, I started setting Viewstate of controls to false. So now I'm getting:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzU3OTU5NDI1ZGTBCxumGHfzsISSVDAeBMbSE7Fvfw==" />

After this I added 3 more labels and likewise set the Viewstate 开发者_StackOverflowproperty for all of these labels to false, but now I am getting same Viewstate bytes.

So I'm a bit confused now in understanding the Viewstate concept.


Let me clarify.

First what is viewstate?

View state's purpose in life is simple: it's there to persist state across postbacks. (By Scott Mitchel)

What does disabling viewstate for Label means?

Disabling viewstate does not mean that when you add Label for the first time in your original aspx file code the values of label will not be added in the viewstate. Disabling actually means the values of viewstate for that particular control will not be changed in the viewstat after the postback.

To elaborate further, when you have disabled viewstate it still save the properties of label. Like the text property that was there for the very first time page was loaded. Also the font it was displayed in and so forth.

What disabling does is when you postback and change the text of Label lets say from Label to "Bye Bye" that information is not going to be saved in the viewstate.

Let me demonstrate with viewstate disabled for Label.

Understanding Viewstate working procedure

When the page initially loads viewstate value is this,

value="/wEPDwULLTExNjMzNDIxNjRkZIajtQgaZZlXAyolkhYpfGy2SBoV"

Then I click on change Label button and in change label button event handler change the labels text to "Bye Bye" viewstate is this,

value="/wEPDwULLTExNjMzNDIxNjRkZIajtQgaZZlXAyolkhYpfGy2SBoV"

Then I click on Empty postback (no code written for this button) button and Label Text changes back to

Label

viewstate value is still,

value="/wEPDwULLTExNjMzNDIxNjRkZIajtQgaZZlXAyolkhYpfGy2SBoV"

Lesson Learned

  1. Disabling viewstate does one thing which is not saving the new state of control after the postback but it doesn't mean it will not save the initial properties.

  2. Disabling viewstate has disadvantage in those situations where the control should persist its state during the postback.

  3. If you know already the control like label text will never be changed, why use Label at all? why don't simply use regular html like


Did you know when label is rendered it html is translated to <span></span>.

Interesting Questions are following

Since we had Label viewstate disabled initially why when we clicked the Change Label button it showed the text changed to "Bye Bye"? Ans. You have to learn how the viewstate works during the page life cycle. For example, following happens when Change Label Button is clicked.

  • Initialization state: Page is initialized from scratch so Label text is set to "Label"

  • View State Load event is called: Nothing happens since viewstate was disabled for Label

  • Raise PostBack events are called: Like the one for Change Button Button_Click() event and Label Text is changed to "Bye Bye".

then some other things follow and page is shown with "Bye Bye" text for label.

  1. Why does Label text changes back to Label when you click Empty Postback button above. Ans.

    • Initialization state: Page is initialized from scratch so Label text is set to "Label"

    • View State Load event is called: Nothing happens since viewstate was disabled for Label

    • Raise PostBack events are called: Like the one for Empty Button's Button_Click() event and since it is not doing anything nothing happens then some other things follow but Label's text is not changed from "Label" during any of those stages.

However if you had Label Viewstate Enabled. Each time postback would have happened Label would have persisted its new state since it would have been changed to new state during Load Viewstate/Save Viewstate phase of page life cycle.


All controls in your webform will add information in ViewState insted you set EnalbedViewState to false. It's the way of asp.net keep the state of the components in the page after each postback because the web and http protocoll is stateless, so the motor of asp.net need to keep the state to create this kind of programmimg (events, components and its properties). Every property of every component that you change of the default value will be on viewstate (if EnabledViewState is true (by default)).

There's a way to compress your viewstate in server-side and descompress it on client-side, transfering data between them easly. Take a look at this: http://www.hanselman.com/blog/ZippingCompressingViewStateInASPNET.aspx

In Asp.Net 4.0 the control of ViewState is more flexible, so I recommend you read it: http://www.codeproject.com/Articles/81489/ViewState-Control-in-ASP-NET-4-0

I recommend you read this to understand the asp.net life cycle better: http://msdn.microsoft.com/en-us/library/ms178472.aspx

I hope it helps! Chees


There are plenty of general articles on viewstate so I won't go into that.

To answer your specific question as to why you aren't seeing a variance in the value of the viewstate hidden field when you add Labels with viewstate enabled/disabled is that viewstate is differential. It tracks non default state in your controls. IE you won't see a change in the viewstate hidden field unless you enable viewstate and make a change to the server control (try just setting the text field).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜