Viewstate Advanced Concepts

Discussion in 'ASP .Net Web Controls' started by Dominic, Dec 29, 2003.

  1. Dominic

    Dominic Guest

    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?
    Dominic, Dec 29, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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?
    Alessandro Zifiglio, Dec 29, 2003
    #2
    1. Advertising

  3. Dominic

    Dominic Guest

    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!
    Dominic, Dec 29, 2003
    #3
  4. 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" <> wrote in message
    news:...
    > 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!
    Alessandro Zifiglio, Dec 29, 2003
    #4
  5. Dominic

    Dominic Guest

    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!
    Dominic, Dec 30, 2003
    #5
  6. Dominic

    Teemu Keiski Guest

    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.aspx?tabindex=1&PostID=250529
    http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=148634

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


    "Dominic" <> wrote in message
    news:...
    > 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!
    Teemu Keiski, Dec 30, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Riken

    OOPS concepts

    Riken, Jul 15, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    17,261
    bmundy
    Aug 6, 2003
  2. Amir Pashazadeh

    .net and descriptive concepts!

    Amir Pashazadeh, Jul 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,038
    Amir Pashazadeh
    Jul 18, 2003
  3. Swathika
    Replies:
    1
    Views:
    517
    red floyd
    Jul 10, 2008
  4. Michele Simionato
    Replies:
    1
    Views:
    584
    Lacrima
    Mar 27, 2010
  5. Trans
    Replies:
    0
    Views:
    145
    Trans
    Apr 23, 2006
Loading...

Share This Page