Enumerate through Controls

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);
}
}
}
 
G

Guest

Try recurring through the controls and adding them to a control collection
first if they are the right type (sorry this is bad code and in VB):

Sub MyRecursiveAdd(ByVal c as Control, ByRef col as Collection)
Dim child as Control
For Each child In c.Controls
If typeof child is TextBox Then
col.Add(child)
ElseIf typeof child is DropDown Then
col.Add(child)
End If
if child.HasControls() Then
MyRecursiveAdd(child,col)
End If
Next
End Sub

After a call to this routine, loop through all the controls in the returned
collection and process them how you show below.
 
B

bruce barker \(sqlwork.com\)

you need to add the controls on postback

-- bruce (sqlwork.com)
 
G

Guest

I was thinking they were not persisting. So, I need to figure out how to make
them persist so I can enumerate through them and then get the value that is
in them on the postback?
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top