Custom control viewstate (?) issue!

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Custom control viewstate (?) issue!

    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 Guest

  2. Similar Questions and Discussions

    1. viewstate and custom control
      I have custom control with dropdownlist. I am using this custom control in a repeater. After the post-back the control properties that i stored in...
    2. Nested control in Web Custom Control and ViewState
      I am working on a Web Custom Control that will function as a Tab menu. I am having problems with figuring out what I need to do to link up the...
    3. Losing viewstate in custom control
      I have a custom control I derived from the datagrid control and set a couple of values in viewstate("foo") = "bar" but on postback they are...
    4. Custom control issue: Where do I put this code?
      Billy, this is probably not the answer to your problem, but shouldn't you check IsPostBack before adding those items? -- John Saunders Internet...
    5. Missing Viewstate in Custom Control
      Easy one for gurus:::: Could someone tell me why this custom control bombs during postback? To duplicate: compile this control, put it in your...
  3. #2

    Default Re: Custom control viewstate (?) issue!

    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:
    [url]http://blogs.aspadvice.com/joteke/archive/2004/03/15/767.aspx[/url]

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

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






    "u3k" <u3k@mail.ru> wrote in message
    news:d9939e67.0405041020.132442cb@posting.google.c om...
    > 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 Guest

  4. #3

    Default Re: Custom control viewstate (?) issue!

    u3k wrote:
    > 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.

    Are you reloading ALL the dynamically added controls on EACH postback?
    > 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
    No. The default value for TextBoxes (and for DropDownLists) does NOT
    use view state at all. I promise.

    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 [email]mitchell@4guysfromrolla.com[/email]

    Happy Programming!

    --

    Scott Mitchell
    [email]mitchell@4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

  5. #4

    Default Re: Custom control viewstate (?) issue!

    "Teemu Keiski" <joteke@aspalliance.com> wrote in message news:<#mzNmrgMEHA.2244@tk2msftngp13.phx.gbl>...
    > 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.Sessi on["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 Guest

  6. #5

    Default Re: Custom control viewstate (?) issue!

    Thanks for your reply!
    I've sent you an email (from: [email]u3k@mail.ru[/email]) 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 [email]mitchell@4guysfromrolla.com[/email]
    >
    > Happy Programming!
    Thanks! I'd love to read it!!!
    u3k Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139