dynamic controls & viewstate

Discussion in 'ASP .Net Web Controls' started by Lachlan James, Nov 16, 2004.

  1. Hi,

    I am attempting to create a questionnaire dynamically in
    an asp.net webform. I have a repeater control which is
    bound to my datasource, the repeater has a placeholder in
    its itemtemplate which I use to add controls to
    dynamically inside the repeaters itemdatabound event.

    I do not know the questions in the questionnaire until
    runtime because they are stored in a database. What I
    would like to do is output the appropriate control
    depending on the question type, this could be a textbox,
    radiobuttonlist, dropdownlist etc.

    At the moment I can get a reference to the placeholder
    control inside the itemdatabound event handler for the
    repeater, then I can add a control to it depending on the
    type of question. This works fine until postback at which
    point the dynamically added controls disappear.

    I know (from reading about the page lifecycle) that
    dynamic controls need to be added to the page each time it
    loads (inside init) in order to preserve viewstate, but
    how can I do this if I don't know which type of control I
    am going to add to the placeholder until the itemdatabound
    event fires? Also later on I would like to be able to add
    more dynamic child question controls depending on the
    users answer.

    I thought of storing the dynamically added controls in an
    ArrayList inside viewstate and then re-adding them to the
    control heirarchy inside the pages Init event handler, but
    that won't work for two reasons. 1: The controls are not
    [Serializable] and 2: I cannot re-add the controls back
    into their exact position in the control tree because they
    were from inside the repeater nested in a placeholder.

    If anyone knows how I could do this, or knows of
    another/better way of doing it I would really like to know.

    Thanks, Lachlan
    Lachlan James, Nov 16, 2004
    1. Advertisements

  2. Lachlan, I'm currently working on an article for MSDN Online that
    answers this very question (i.e., shows how to build a dynamic data
    entry user interface). If you shoot me an email -
    - I can send you the article as it currently
    is. (It should be published online at
    http://msdn.microsoft.com/asp.net/ within a month or so).

    In any event, another option is to use an application like nSurvey -



    Scott Mitchell


    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Nov 17, 2004
    1. Advertisements

  3. mezza

    mezza Guest

    James did you get an answer to this?
    I am using exactly the same setup as you, but onpostback when I try to
    grab the dynamic control from the RepeaterItem.PlaceHolder the
    Placeholder contains nothing. I think it is do with viewstate, but
    using a Repeater, PlaceHolder and dynamic controls I don't know when to
    access the repeater and rebuild the controls so that when LoadViewState
    and LoadPostBack kick in I have the relevant dynamic controls.

    Posted via http://www.mcse.ms
    View this thread: http://www.mcse.ms/message1226547.html
    mezza, May 5, 2006
    1. Advertisements

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. Abel

    dynamic controls and viewstate?

    Abel, Jun 30, 2003, in forum: ASP .Net
    Axel Dahmen
    Jul 1, 2003
  2. Tom Wisnowski
    Jacob Yang [MSFT]
    Dec 6, 2003
  3. Chuck Ritzke
    Chuck Ritzke
    Aug 6, 2004
  4. Ronald
    Robbe Morris [C# MVP]
    Jan 17, 2006
  5. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    May 4, 2004

Share This Page