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