Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

problems understanding ViewState

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Peeters - 13 Aug 2007 09:56 GMT
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
Ladislav Mrnka - 13 Aug 2007 10:18 GMT
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

> Hi,
>
[quoted text clipped - 31 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Chris Peeters - 13 Aug 2007 11:14 GMT
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
Ladislav Mrnka - 13 Aug 2007 12:08 GMT
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

> Hi Ladislav,
>
[quoted text clipped - 8 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Milosz Skalecki [MCAD] - 13 Aug 2007 13:28 GMT
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
Signature

Milosz

> Hi Chris,
>
[quoted text clipped - 33 lines]
> >
> > *** Sent via Developersdex http://www.developersdex.com ***
Chris Peeters - 13 Aug 2007 13:51 GMT
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
Ladislav Mrnka - 13 Aug 2007 14:48 GMT
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

> Hello Ladislav,
>
[quoted text clipped - 8 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.