G
Guest
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(Question.BuildingBlock buildingBlock in
questions.QuestionBuildingBlocks)
{
Label bbLabel = new Label();
bbLabel.Text = "<p>" + buildingBlock.Identifier + ". " +
buildingBlock.BuildingBlockText + "<br><br>";
phBuildingBlocks.Controls.Add(bbLabel);
switch(buildingBlock.BuildingBlockType.ToString())
{
case "Radio":
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.ID = "survey_question_" +
buildingBlock.Identifier.Trim();
radioButtonList.DataValueField = "AnswerOptionID";
radioButtonList.DataTextField = "AnswerOption";
radioButtonList.DataSource =
Answer.ListOptions(buildingBlock.AnswerGroupID);
radioButtonList.DataBind();
phBuildingBlocks.Controls.Add(radioButtonList);
break;
case "DropDown":
DropDownList dropDownList = new DropDownList();
dropDownList.ID = "survey_question_" +
buildingBlock.Identifier.Trim();
dropDownList.DataValueField = "AnswerOptionID";
dropDownList.DataTextField = "AnswerOption";
dropDownList.DataSource =
Answer.ListOptions(buildingBlock.AnswerGroupID);
dropDownList.DataBind();
phBuildingBlocks.Controls.Add(dropDownList);
break;
case "CheckBoxes":
CheckBoxList checkBoxList = new CheckBoxList();
checkBoxList.ID = "survey_question_" +
buildingBlock.Identifier.Trim();
checkBoxList.DataValueField = "AnswerOptionID";
checkBoxList.DataTextField = "AnswerOption";
checkBoxList.DataSource =
Answer.ListOptions(buildingBlock.AnswerGroupID);
checkBoxList.DataBind();
phBuildingBlocks.Controls.Add(checkBoxList);
break;
default: //Text
TextBox textBox = new TextBox();
textBox.ID = "survey_question_" + buildingBlock.Identifier.Trim();
textBox.MaxLength = 8000;
textBox.Width = 600;
textBox.TextMode = TextBoxMode.MultiLine;
textBox.Rows = 5;
phBuildingBlocks.Controls.Add(textBox);
break;
}
}
}
}
private void SaveAnswers()
{
IterateThroughChildren(this);
}
private void IterateThroughChildren(Control parent)
{
foreach (Control c in phBuildingBlocks.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
Response.Write(c.ID);
}
if (c.Controls.Count > 0)
{
IterateThroughChildren(c);
}
}
}
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(Question.BuildingBlock buildingBlock in
questions.QuestionBuildingBlocks)
{
Label bbLabel = new Label();
bbLabel.Text = "<p>" + buildingBlock.Identifier + ". " +
buildingBlock.BuildingBlockText + "<br><br>";
phBuildingBlocks.Controls.Add(bbLabel);
switch(buildingBlock.BuildingBlockType.ToString())
{
case "Radio":
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.ID = "survey_question_" +
buildingBlock.Identifier.Trim();
radioButtonList.DataValueField = "AnswerOptionID";
radioButtonList.DataTextField = "AnswerOption";
radioButtonList.DataSource =
Answer.ListOptions(buildingBlock.AnswerGroupID);
radioButtonList.DataBind();
phBuildingBlocks.Controls.Add(radioButtonList);
break;
case "DropDown":
DropDownList dropDownList = new DropDownList();
dropDownList.ID = "survey_question_" +
buildingBlock.Identifier.Trim();
dropDownList.DataValueField = "AnswerOptionID";
dropDownList.DataTextField = "AnswerOption";
dropDownList.DataSource =
Answer.ListOptions(buildingBlock.AnswerGroupID);
dropDownList.DataBind();
phBuildingBlocks.Controls.Add(dropDownList);
break;
case "CheckBoxes":
CheckBoxList checkBoxList = new CheckBoxList();
checkBoxList.ID = "survey_question_" +
buildingBlock.Identifier.Trim();
checkBoxList.DataValueField = "AnswerOptionID";
checkBoxList.DataTextField = "AnswerOption";
checkBoxList.DataSource =
Answer.ListOptions(buildingBlock.AnswerGroupID);
checkBoxList.DataBind();
phBuildingBlocks.Controls.Add(checkBoxList);
break;
default: //Text
TextBox textBox = new TextBox();
textBox.ID = "survey_question_" + buildingBlock.Identifier.Trim();
textBox.MaxLength = 8000;
textBox.Width = 600;
textBox.TextMode = TextBoxMode.MultiLine;
textBox.Rows = 5;
phBuildingBlocks.Controls.Add(textBox);
break;
}
}
}
}
private void SaveAnswers()
{
IterateThroughChildren(this);
}
private void IterateThroughChildren(Control parent)
{
foreach (Control c in phBuildingBlocks.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
Response.Write(c.ID);
}
if (c.Controls.Count > 0)
{
IterateThroughChildren(c);
}
}
}