I am creating a questionnaire that has its controls dynamically created within a user control. I am hooking the creation of this controls on the usercontrol page_init event.
My question is how can I parse through all the controls on the form and get the user's selected answers? Note also that the questionnaire will have textboxes, radiobuttonlists, dropdowns and other controls. Should I create the controls with their ID as
the Question's primary key?
publicvoid CreateInstanceOf(string controlType, IEnumerable<ANSWER> answers, UserControl userControl)
{
var getTypeByStringName = TypeMapper(controlType);
if (getTypeByStringName != null)
{
var newControl = Activator.CreateInstance(TypeMapper(controlType));
var castedControl = newControl asControl;
if (castedControl != null)
{
if (castedControl isDropDownList)
{
var dropDown = castedControl asDropDownList;
foreach (var answer in answers)
{
dropDown.Items.Add(newListItem {Text = answer.ANSWER1, Value = answer.VALUE});
dropDown.ID = answer.Q_ID.ToString(); //Since all Question IDS are the same, this should work just fine
}
dropDown.ClientIDMode = ClientIDMode.Static;
dropDown.Items.Insert(0, newListItem { Text = "Select", Value = "" });
dropDown.Items.FindByText("Select").Selected = true;
userControl.Controls.Add((Control) newControl);
}
elseif (castedControl isRadioButtonList)
{
var radioButtonList = castedControl asRadioButtonList;
foreach (var answer in answers)
{
radioButtonList.Items.Add(newListItem { Text = answer.ANSWER1, Value = answer.VALUE });
radioButtonList.ID = answer.Q_ID.ToString(); //Since all Question IDS are the same, this should work just fine
}
radioButtonList.ClientIDMode = ClientIDMode.Static;
userControl.Controls.Add((Control)newControl);
}
}
}
}
amosCabanban...
Member
441 Points
148 Posts
Dynamic Creation of Controls in a questionnaire
May 05, 2012 08:57 PM|LINK
I am creating a questionnaire that has its controls dynamically created within a user control. I am hooking the creation of this controls on the usercontrol page_init event.
My question is how can I parse through all the controls on the form and get the user's selected answers? Note also that the questionnaire will have textboxes, radiobuttonlists, dropdowns and other controls. Should I create the controls with their ID as the Question's primary key?