DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Mon, 13 Aug 2007 01:56:50 -0700,    posted on: microsoft.public.dotnet.framework.aspnet        back       

Thread Index
  1    Chris Peeters
          2    Ladislav Mrnka
                 3    Chris Peeters
          4    Ladislav Mrnka
                 5    Milosz Skalecki [MCAD]
                 6    Chris Peeters
                        7    Ladislav Mrnka


problems understanding ViewState   
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




*** Sent via Developersdex http://www.developersdex.com ***
Date:Mon, 13 Aug 2007 01:56:50 -0700   Author:  

RE: problems understanding ViewState   
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

"Chris Peeters" wrote:


> 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
> 
> 
> 
> 
> *** Sent via Developersdex http://www.developersdex.com ***
> 
Date:Mon, 13 Aug 2007 02:18:05 -0700   Author:  

RE: problems understanding ViewState   
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



*** Sent via Developersdex http://www.developersdex.com ***
Date:Mon, 13 Aug 2007 03:14:47 -0700   Author:  

RE: problems understanding ViewState   
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

"Chris Peeters" wrote:


> 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
> 
> 
> 
> *** Sent via Developersdex http://www.developersdex.com ***
> 
Date:Mon, 13 Aug 2007 04:08:01 -0700   Author:  

RE: problems understanding ViewState   
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
-- 
Milosz


"Ladislav Mrnka" wrote:


> 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
> 
> "Chris Peeters" wrote:
> 
> > 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
> > 
> > 
> > 
> > *** Sent via Developersdex http://www.developersdex.com ***
> > 
Date:Mon, 13 Aug 2007 05:28:04 -0700   Author:  

RE: problems understanding ViewState   
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



*** Sent via Developersdex http://www.developersdex.com ***
Date:Mon, 13 Aug 2007 05:51:48 -0700   Author:  

RE: problems understanding ViewState   
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

"Chris Peeters" wrote:


> 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
> 
> 
> 
> *** Sent via Developersdex http://www.developersdex.com ***
> 
Date:Mon, 13 Aug 2007 06:48:02 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us