I have some controls that I am creating dynamically. After the user finishes
entering their answers in these controls, I would like to iterate through
these controls and get the answers out. I have tried a couple of ways, but do
not see those controls there. Can someone please help? Below is snippett of
code to show how I am adding the controls and "for now" how I am trying to
read them back.
foreach(Questio n.BuildingBlock buildingBlock in
questions.Quest ionBuildingBloc ks)
{
Label bbLabel = new Label();
bbLabel.Text = "<p>" + buildingBlock.I dentifier + ". " +
buildingBlock.B uildingBlockTex t + "<br><br>";
phBuildingBlock s.Controls.Add( bbLabel);
switch(building Block.BuildingB lockType.ToStri ng())
{
case "Radio":
RadioButtonList radioButtonList = new RadioButtonList ();
radioButtonList .ID = "survey_questio n_" +
buildingBlock.I dentifier.Trim( );
radioButtonList .DataValueField = "AnswerOptionID ";
radioButtonList .DataTextField = "AnswerOpti on";
radioButtonList .DataSource =
Answer.ListOpti ons(buildingBlo ck.AnswerGroupI D);
radioButtonList .DataBind();
phBuildingBlock s.Controls.Add( radioButtonList );
break;
case "DropDown":
DropDownList dropDownList = new DropDownList();
dropDownList.ID = "survey_questio n_" +
buildingBlock.I dentifier.Trim( );
dropDownList.Da taValueField = "AnswerOptionID ";
dropDownList.Da taTextField = "AnswerOpti on";
dropDownList.Da taSource =
Answer.ListOpti ons(buildingBlo ck.AnswerGroupI D);
dropDownList.Da taBind();
phBuildingBlock s.Controls.Add( dropDownList);
break;
case "CheckBoxes ":
CheckBoxList checkBoxList = new CheckBoxList();
checkBoxList.ID = "survey_questio n_" +
buildingBlock.I dentifier.Trim( );
checkBoxList.Da taValueField = "AnswerOptionID ";
checkBoxList.Da taTextField = "AnswerOpti on";
checkBoxList.Da taSource =
Answer.ListOpti ons(buildingBlo ck.AnswerGroupI D);
checkBoxList.Da taBind();
phBuildingBlock s.Controls.Add( checkBoxList);
break;
default: //Text
TextBox textBox = new TextBox();
textBox.ID = "survey_questio n_" + buildingBlock.I dentifier.Trim( );
textBox.MaxLeng th = 8000;
textBox.Width = 600;
textBox.TextMod e = TextBoxMode.Mul tiLine;
textBox.Rows = 5;
phBuildingBlock s.Controls.Add( textBox);
break;
}
}
}
}
private void SaveAnswers()
{
IterateThroughC hildren(this);
}
private void IterateThroughC hildren(Control parent)
{
foreach (Control c in phBuildingBlock s.Controls)
{
if (c.GetType().To String().Equals ("System.Web.UI .WebControls.Te xtBox"))
{
Response.Write( c.ID);
}
if (c.Controls.Cou nt 0)
{
IterateThroughC hildren(c);
}
}
}