dynamic controls & viewstate

L

Lachlan James

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
 
S

Scott Mitchell [MVP]

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 -
(e-mail address removed) - 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 -
http://www.nsurvey.org

hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
M

mezza

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.
Regards,
Mezza
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top