problems understanding ViewState

C

Chris Peeters

Hi,

I have a Listbox with 3 strings added at design time : "Red" "Green"
"Blue".
I add a fourth string at run-time in Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
lstColors.Items.Add("Yellow");
}

Enabling the EnableViewState-property of the Listbox always shows the 4
strings.
Disabling the EnableViewState-property never shows "Yellow".

I find it very confusing: all 4 string are part of Listbox's ViewState
no ?
I'd say, or you see all 4 of them (when EnableViewState=true) or you
don't see anything listed (when EnableViewState=false).

But it doesn't work that way apparently. What is the logic behind
EnableViewState ?

It is confusing as well when using a Label: with EnableViewState set or
not, the Text property is ALWAYS shown, even when that text was set at
runtime.

So, what does ViewState really mean for a Control (ListBox, Label, ...)
?

Thank you
Chris
 
G

Guest

Hi Chris,

if you turn off view state for your list box you should see the yellow item
only in first request to your page because you fill it in only when page is
not posted back. View state is actually a hidden field where all registered
controls store their state from previous request (encoded and usually
encrypted). This state is used when next request is precessed so if you turn
on view state all four items are stored to view state. When next request
arrives (post back) asp.net engine knows that list box contains four items
but if you turn off the view state asp.net engine does not have this
information and it has to fill list box again. But at this time only three
items with declarative definition are filled because yellow item is added
only if page was not posted back.

View state is usually turned off for controls binded to large dataset to
reduce size of http response. But there you have to rebind them in each time.

I hope this explanation helps you to understand the view state.

Regards,
Ladislav
 
C

Chris Peeters

Hi Ladislav,

thank you for your explanation !

I understand the Postback mechanism but what is still unclear to me is
why are the 3 other strings (red, green, blue) shown even when
EnableViewState is turned off ?

thank you
Chris
 
G

Guest

Hi Chris,

as you wrote at the first post you have added items in design time. It means
those items are defined in source view of your aspx file. It should looks
like:
<asp:ListBox ID="lstColors" runat="server" EnableViewState="false">
<asp:ListItem Text="Red" />
<asp:ListItem Text="Green" />
<asp:ListItem Text="Blue" />
</asp:ListBox>
Whole aspx file is parsed and compiled when it is requested first time.
Parsed means your page will become new class which inherits from your code
behind class. Aspx file is just description of components and their initial
settings for this new class.

When request is processed instance of that page class is created (= all
controls are initialized). Initilazation of controls is based on you
declaration in aspx or on view state - depends on your view state settings.

This is really brief and inexact description of what is happening inside but
I hope it will help you to understand it.

Regards,
Ladislav
 
G

Guest

Hi there,

In addition to what Ladislav has said, all the values specified in the aspx
code are used by the compiler as initial values and are not kept in the view
state (this is a very logical improvement to whole viewstate concept). All
the controls have TrackViewState method that internally sets
IsTrackingViewState flag to force an item to be serialized in the viewstate
(SetItemDirty method of the StateBag class). So the key thing you need to
know is mentioned property is set to false before any control is added to the
page. Therefore, all the items added to Items collection before listbox is
added to the page will not be stored in viewstate. To see how it's done, have
a look at the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\yourwebsitename directory. You should find some compiled .cs files that
represent your pages:


private global::System.Web.UI.WebControls.ListBox @__BuildControllstColors() {
global::System.Web.UI.WebControls.ListBox @__ctrl;

#line 62 "C:\Documents and Settings\Mily\My Documents\Visual Studio
2005\WebSites\TestWebsite\Default2.aspx"
@__ctrl = new global::System.Web.UI.WebControls.ListBox();

#line default
#line hidden
this.lstColors = @__ctrl;
@__ctrl.ApplyStyleSheetSkin(this);

#line 62 "C:\Documents and Settings\Mily\My Documents\Visual Studio
2005\WebSites\TestWebsite\Default2.aspx"
@__ctrl.ID = "lstColors";

#line default
#line hidden

#line 62 "C:\Documents and Settings\Mily\My Documents\Visual Studio
2005\WebSites\TestWebsite\Default2.aspx"
@__ctrl.EnableViewState = true;

#line default
#line hidden

#line 62 "C:\Documents and Settings\Mily\My Documents\Visual Studio
2005\WebSites\TestWebsite\Default2.aspx"
this.@__BuildControl__control9(@__ctrl.Items);

#line default
#line hidden
return @__ctrl;
}




#line default
#line hidden
global::System.Web.UI.WebControls.ListBox @__ctrl13;

#line 10 "C:\Documents and Settings\Mily\My Documents\Visual Studio
2005\WebSites\TestWebsite\Default2.aspx"
@__ctrl13 = this.@__BuildControllstColors();

#line default
#line hidden

#line 10 "C:\Documents and Settings\Mily\My Documents\Visual Studio
2005\WebSites\TestWebsite\Default2.aspx"
@__parser.AddParsedSubObject(@__ctrl13);
// starting from now on, trackingviewstate is set to true so all the changes
will affect the viewstate, but as you could see your three items had been
added before (
..@__BuildControl__control9(@__ctrl.Items) adds three new items represeting
your color)

HTH
 
C

Chris Peeters

Hello Ladislav,

it's clearer now for the Listbox !

Maybe a final question: what about when using a Label: with
EnableViewState set or not for the label-control, the Text property is
ALWAYS shown, even when that text was set at runtime ???

thank you
Chris
 
G

Guest

Hi Chris,

Label is processed in same way as any other web control. If you set Text
attribute in asp:Label tag (means you set the Text in design time) it will
always be used as initialization. ViewState stores only values assigned at
runtime. Also if you use something like this:
protected void Page_Load(object sender, EventArgs e)
{
MyLabel.Text = "some text";
}
you will override the value (at runtime) in each request = ViewState can be
turned off and behavior will be the same as when you turn the ViewState on
and use:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyLabel.Text = "some text";
}
}

Regards,
Ladislav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top