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 ***