Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Viewstate Advanced Concepts

Reply
Thread Tools

Viewstate Advanced Concepts

 
 
Dominic
Guest
Posts: n/a
 
      12-29-2003
This post is not without research. I have an informed understanding
of the Viewstate concept, but I'm missing something fundamental.
Here's what I've got:

Control properties not explicitly defined in page design, those not
included in the http post, or those that have changed should be stored
and persisted through viewstate.

Now when it comes to dynamic controls, I'm of the understanding that
all properties of dynamic controls are persisted in viewstate as long
as on postback they are added at the same position in the control
tree.

Testing this understanding in the most simple case, I'm proven wrong.
With a page design of a placeholder, and a button for postback, my
page_load code is:

Dim MyLabel As Label = New Label
If Not IsPostBack Then MyLabel.Text = "Hello World"
PlaceHolder1.Controls.Add(MyLabel)

When clicking the button on the page, the label goes blank, meaning
the initial .text value for the label was not saved and persisted. Am
I only under the mistaken assumption that this should be implicit and
rather, I am to create a custom label control whereas I need to
override all properties I want persisted and manually manage
Viewstate? Or am I missing something?
 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      12-29-2003
add the label to the placeholder control even after postback. For
dynamically added controls this is a requirement. Your dynamically added
control maintains state, meaning that adding the control to the place holder
after postback does not cause it to loose its state.



"Dominic" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> This post is not without research. I have an informed understanding
> of the Viewstate concept, but I'm missing something fundamental.
> Here's what I've got:
>
> Control properties not explicitly defined in page design, those not
> included in the http post, or those that have changed should be stored
> and persisted through viewstate.
>
> Now when it comes to dynamic controls, I'm of the understanding that
> all properties of dynamic controls are persisted in viewstate as long
> as on postback they are added at the same position in the control
> tree.
>
> Testing this understanding in the most simple case, I'm proven wrong.
> With a page design of a placeholder, and a button for postback, my
> page_load code is:
>
> Dim MyLabel As Label = New Label
> If Not IsPostBack Then MyLabel.Text = "Hello World"
> PlaceHolder1.Controls.Add(MyLabel)
>
> When clicking the button on the page, the label goes blank, meaning
> the initial .text value for the label was not saved and persisted. Am
> I only under the mistaken assumption that this should be implicit and
> rather, I am to create a custom label control whereas I need to
> override all properties I want persisted and manually manage
> Viewstate? Or am I missing something?



 
Reply With Quote
 
 
 
 
Dominic
Guest
Posts: n/a
 
      12-29-2003
The label IS added to the placeholder on every page_load in the code
example. The conditional postback just sets the .text value on the
first page_load. My understanding is that with that property set, it
should persist on every postback so long as a label is again added to
the placeholder. Why, in the example, on postback, is the .text value
of the label lost?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      12-29-2003
ok, i didnt see you if statement ending. I had looked at your code too
quick.
Another common mistake when adding controls dynamically is to set the
control values after they have been added dynamically. In your case after
having added the control, only then start setting properties, otherwise it
wont maintain its previous state :

This is how you need to do it :

Dim MyLabel As Label = New Label()
PlaceHolder1.Controls.Add(MyLabel)
If Not IsPostBack Then MyLabel.Text = "Hello World"

"Dominic" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The label IS added to the placeholder on every page_load in the code
> example. The conditional postback just sets the .text value on the
> first page_load. My understanding is that with that property set, it
> should persist on every postback so long as a label is again added to
> the placeholder. Why, in the example, on postback, is the .text value
> of the label lost?
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
Dominic
Guest
Posts: n/a
 
      12-30-2003
Ah, that was the trick. Gotta add to the control tree BEFORE setting
any properties. Thank you for the help, much appreciated.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      12-30-2003
Yes,

reason for this is that control lifecycle (including viewstate tracking)
starts only after control is added to the control tree, not before.
Therefore values set before are considered as initial values and are not
saved to ViewState. If you are interested to know more see:
http://www.asp.net/Forums/ShowPost.a...&PostID=250529
http://www.asp.net/Forums/ShowPost.a...&PostID=148634

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


"Dominic" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ah, that was the trick. Gotta add to the control tree BEFORE setting
> any properties. Thank you for the help, much appreciated.
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Advanced Design Concepts in J2EE ( Exceptions, Abstract Classes andDesign Issues & Solutions) Swathika C++ 1 07-10-2008 05:46 PM
Concepts, concepts, concepts... Trans Ruby 0 04-23-2006 11:56 AM



Advertisments