locked
Dynamic Controls and ViewState RRS feed

  • Question

  • User2073724220 posted

    I am trying to create a highly dynamic control. Here is the setup:

    DropDownList
    Label     Textbox
    Label     Textbox     Validator
    Label     Textbox

    The key with this setup is that depending on the selection that is made in the DropDownList the order that the controls below it will change. I had this all setup in a Custom Server Control, but it doesn't work. Sadly I get the following error:

    Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

    I did some reasearch and it seems that if your control is being dynamically added it should be added to the end of the controls collection. The problem I see with my implementation is that all of the controls (except the dropdownlist) can be added in any order.

    I am wondering how other people have created highly dynamic like this before. Is there some trick?

    Monday, July 7, 2008 6:03 PM

Answers

  • User2073724220 posted

     Ok, so I may have solved the relatively quickly. Basically, I ensured that in CreateChildControls I rewire the controls exactly the way they were from before. Then later on in the dropdownlist event handler I call Controls.Clear() and reassemble the controls in a new order. It seems to work.

    Thoughts?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 6:46 PM

All replies

  • User2073724220 posted

     Ok, so I may have solved the relatively quickly. Basically, I ensured that in CreateChildControls I rewire the controls exactly the way they were from before. Then later on in the dropdownlist event handler I call Controls.Clear() and reassemble the controls in a new order. It seems to work.

    Thoughts?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 6:46 PM
  • User481221548 posted

    Hi csdietrich

    What`s about get rid of Viewstate for your dynamic controls (TextBox and Label).

    edit: Oh, thats fine :)

    Monday, July 7, 2008 6:47 PM