Custom control viewstate (?) issue!

Discussion in 'ASP .Net Building Controls' started by u3k, May 4, 2004.

  1. u3k

    u3k Guest

    I've been struggling for a day and yet haven't found a solution to
    such a problem:

    I have, let's say, an apsx page, which contains a UserControl, which
    adds to it's Controls collection a new control, dynamically loaded via
    LoadControl, which in order is also a UserControl, visually designed.
    When I init this 'dynamic control', i set properties of this 'dynamic'
    control's subscontrols (textboxes, labels - all WebControls), on first
    load i see this initial, desired, values. But then, after a post back
    - those initial value are lost. All viewstate stuff is set properly
    thru the whole hierarcy of controls. The weird thing is that even if I
    add any text to textboxes containig initial values in browser, e.g.
    Initial value was 'Default', and i type up to the end of the string
    '!!!!', after postback i get in textbox just '!!!!'.... Seems like
    viewstate is restored but in some irrational way - only what is
    received in postback! More: even if i will mix the parts of initial
    value in browser text box, for example - switch part of the phrase via
    cut and paste, theese parts also will be removed from viewstate. And
    more: if i'll retype the exact string in browser textbox, the same
    that was written in intialization part, but before that _clear_
    (DEL-DEL-DEL:) textbox's contents, the string _typed_ is restored...
     
    u3k, May 4, 2004
    #1
    1. Advertising

  2. u3k

    Teemu Keiski Guest

    Hi,

    Do you add the control to Controls collection in Page_Init? If you do, you
    need to set initial values on every request, because ViewState tracking
    starts after Init.

    Same concerns if you first set initial values when loading the control with
    LoadControl but you haven't yet added the control to Controls collection.
    Control lifecycle, which includes ViewState tracking etc, begins only after
    control is added to the Controls collection.

    TextBox returning the postback data, is normal, that is how it updates its
    state. It updates the value in ViewState with the posted value. I've
    explained some of the mechanics of it here:
    http://blogs.aspadvice.com/joteke/archive/2004/03/15/767.aspx

    Here's also threads at ASP.NEt Forums related to dynamical controls:
    http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=391019
    http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=285389

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke






    "u3k" <> wrote in message
    news:...
    > I've been struggling for a day and yet haven't found a solution to
    > such a problem:
    >
    > I have, let's say, an apsx page, which contains a UserControl, which
    > adds to it's Controls collection a new control, dynamically loaded via
    > LoadControl, which in order is also a UserControl, visually designed.
    > When I init this 'dynamic control', i set properties of this 'dynamic'
    > control's subscontrols (textboxes, labels - all WebControls), on first
    > load i see this initial, desired, values. But then, after a post back
    > - those initial value are lost. All viewstate stuff is set properly
    > thru the whole hierarcy of controls. The weird thing is that even if I
    > add any text to textboxes containig initial values in browser, e.g.
    > Initial value was 'Default', and i type up to the end of the string
    > '!!!!', after postback i get in textbox just '!!!!'.... Seems like
    > viewstate is restored but in some irrational way - only what is
    > received in postback! More: even if i will mix the parts of initial
    > value in browser text box, for example - switch part of the phrase via
    > cut and paste, theese parts also will be removed from viewstate. And
    > more: if i'll retype the exact string in browser textbox, the same
    > that was written in intialization part, but before that _clear_
    > (DEL-DEL-DEL:) textbox's contents, the string _typed_ is restored...
     
    Teemu Keiski, May 4, 2004
    #2
    1. Advertising

  3. u3k

    u3k Guest

    "Teemu Keiski" <> wrote in message news:<#>...
    > Hi,
    >
    > Do you add the control to Controls collection in Page_Init? If you do, you
    > need to set initial values on every request, because ViewState tracking
    > starts after Init.
    >
    > Same concerns if you first set initial values when loading the control with
    > LoadControl but you haven't yet added the control to Controls collection.
    > Control lifecycle, which includes ViewState tracking etc, begins only after
    > control is added to the Controls collection.


    Thanks! It made much clearer to me what happens for dynamic controls,
    but... the problem want's there, i've discovered...

    What happens: i have several objects placed on Session. After user
    logs in, his objects (custom object taht contains user data,
    Session["User"]) is put on Session. It lives in in intact just down to
    logout. But there are objects that are refreshed each request - e.g.
    Session["CurrentObject"], wich represents object whic corresponds a
    node selected in website tree. Each requsest Session["CurrentObject"]
    is replaced with new refernce to object. So, when in dynamic control I
    init values with data from Session["User"]:

    TextBox1.Text = ((Controller.ProxyObject)HttpContext.Current.Session["User"]).Login;

    .... it all works fine!

    but when i use (...HttpContext.Current.Session["CurrenObject"]).ObjectName,
    in other words use refreshed object to initalize Text property, i get
    behaivoir i've described. I've tried to catch wether control tries to
    initialize property when with corrupt data, buti failed - data was
    always available.

    Weird! How can it be explained?

    P.S. Thanks again for links - they had an emmense effect!
     
    u3k, May 5, 2004
    #3
  4. u3k

    u3k Guest

    Thanks for your reply!
    I've sent you an email (from: ) with description of my
    problem
    more detailed - i've found that it is a bit more cumbersum problem
    that i've expected:)

    > I have an article coming up on MSDN about ASP.NET View State. It's not
    > yet published, but if you drop me your email address, I'd be happy to
    > send you a draft copy of the article. I think it would help clear up a
    > lot of misconceptions and strengthen your understanding of view state.
    > If you are interested, drop me a line at
    >
    > Happy Programming!


    Thanks! I'd love to read it!!!
     
    u3k, May 5, 2004
    #4
    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. Ben Rush
    Replies:
    2
    Views:
    573
    Ben Rush
    Dec 5, 2003
  2. Tracker1 - Michael J. Ryan

    Weird ViewState issue in a Custom Control

    Tracker1 - Michael J. Ryan, Dec 1, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    509
    Tracker1 - Michael J. Ryan
    Dec 2, 2005
  3. Matt Sokol
    Replies:
    2
    Views:
    426
    Theo Tillotson
    Aug 7, 2003
  4. whoisnidhee

    ViewState of the Composite Control composed of custom control

    whoisnidhee, Jul 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    332
    Nidhee
    Jul 16, 2004
  5. Tracker1 - Michael J. Ryan

    Weird ViewState issue in a Custom Control

    Tracker1 - Michael J. Ryan, Dec 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    107
    Tracker1 - Michael J. Ryan
    Dec 1, 2005
Loading...

Share This Page