LA.NET [EN]

Dec 03

Everybody knows that using view state means that you’ll go to hell when you die….nonetheless, many keeps using it, even after having been warned about it (probably because they find heaven to dull??? who knows :) ,,)…to be honest, the default opt-out model we had until now didn’t really helped. The good news is that ASP.NET 4.0 introduces a new model known as opt-in model. So, what’s the idea behind it? A quick recap of what we had is in order…until 4.0, view state was controlled through the EnableViewState property. Whenever you set it to false, you’re saying that that control and all its children won’t persist any data in view state.

Now, this caused all sorts of quirks in v1.0 because programmers would turned it off at the page level (less KBs in a page is good, right?) and all hell broke loose because server controls stopped working. The workaround was simple: leave it on by default at the page level and turn it off in all the controls that don’t need it (btw, this was called the opt-out model). Even though ASP.NET 2.0 introduced control state to make controls work without relying on view state, the truth is that existing opt-out view state model still sucked in the real world because 1.) people are lazy and/or 2.) there was always someone who ended up forgetting to disable view state in the controls they’ve added to the page.

Now, things are better with ASP.NET 4.0 and its new opt-in model. In order to make it work, they’ve added a ViewStateMode property, which gets its value from an enum with the same name.Currently, it supports the these values:

  • Inherit: inherits the value of the property from its control parent. This is the default value;
  • Enabled: enables view state in the current control (even if the parent has its ViewStateMode set to Disabled);
  • Disabled: disables view state in the current control.

Before going, you must understand that this property will only be of use when the EnableViewState property is set to true (when it’s set to false, view state is off for that control and all the controls on its control tree). Now, lets see how we can use this new property and the new opt-in model.

By default, ViewStateMode is set to inherit (default value inherited from the Control class itself – which, btw, also sets EnableViewState to true). Now, if you set the ViewStateMode at page level to Disabled, you can turn it on only for the controls that need it by setting that property to enabled on those controls. This is the new (and let me add this: cool!) opt-in model introduced by ASP.NET 4.0.

Final note: If you debug an ASP.NET page, you’ll notice that Inherits is still the value of the ViewStateMode property…if that is the case, then view state shouldn’t be on by default, right? After all, Page is the top control in the control tree and the docs clearly say that view state is on only when the ViewState is set to Enabled or when a parent control has that value and the current one has its mode set to Inherits…so, “what gives?” (reaped shamelessly from October Road, another great TV show which ended abruptly…it seems like these ABC guys terminate all the series I like…oh, lets leave this whining to a future post)

The answer lies on the private SaveAllState method, which is responsible for persisting view state info into the hidden field (the default behavior). When it sees that the ViewStateMode of the page is set to Inherits, it will automatically use Enabled instead. And that’s it. I’ll try to come back with more on ASP.NET.

2 comments so far

  1. sxecwkuy
    12:26 am - 12-13-2009

    RvSJmy bstoiodkvwgi, [url=http://vdtkhccmeenb.com/]vdtkhccmeenb[/url], [link=http://eisdvxrpciox.com/]eisdvxrpciox[/link], http://fccogolgxdca.com/

  2. angkhjc
    12:29 am - 12-13-2009

    ieLjVC rspwckvtbqmn, [url=http://tufwehtkjdzb.com/]tufwehtkjdzb[/url], [link=http://qpxchdjjwdqb.com/]qpxchdjjwdqb[/link], http://qbzfubdnggst.com/

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>