473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Enumerate through Controls

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.WebC ontrols.TextBox"))
{
Response.Write(c.ID);
}

if (c.Controls.Count 0)
{
IterateThroughChildren(c);
}
}
}

Aug 4 '06 #1
3 1661
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.

"Mike Collins" wrote:
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.WebC ontrols.TextBox"))
{
Response.Write(c.ID);
}

if (c.Controls.Count 0)
{
IterateThroughChildren(c);
}
}
}
Aug 4 '06 #2
you need to add the controls on postback

-- bruce (sqlwork.com)
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:C4**********************************@microsof t.com...
>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.WebC ontrols.TextBox"))
{
Response.Write(c.ID);
}

if (c.Controls.Count 0)
{
IterateThroughChildren(c);
}
}
}

Aug 4 '06 #3
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?

"bruce barker (sqlwork.com)" wrote:
you need to add the controls on postback

-- bruce (sqlwork.com)
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:C4**********************************@microsof t.com...
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.WebC ontrols.TextBox"))
{
Response.Write(c.ID);
}

if (c.Controls.Count 0)
{
IterateThroughChildren(c);
}
}
}


Aug 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MuZZy | last post by:
Hi, I need to build a function which would return all controls which are upper in z-order relatively to given control. I need this in order to be able to temporary bring the control to front...
7
by: localhost | last post by:
What is the best and/or fastest way to enumerate attributes of an HTML control? In my page template, I have: <body id="myBody" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0"...
0
by: The Colonel | last post by:
What's the best way to enumerate datagrid controls to build a SQL statement for updates and inserts?
5
by: HL | last post by:
Hi, I need to enumerate windows and find the sum of the rect of all the windows of a specific application. In C++, I use the APIs - 'EnumWindows , GetWindowRect and UnionRect to accomplish the...
3
by: J | last post by:
I'm trying to make a list of certain properties of all the controls on an ASP.NET page. Does anyone know of an easy way to do this? I've tried to do it a couple of different ways but I'm not...
1
by: smichr | last post by:
I see that there is a thread of a similar topic that was posted recently ( enumerate with a start index ) but thought I would start a new thread since what I am suggesting is a little different. ...
1
by: gjohncock | last post by:
I am working on a project that requires me to be able to enumerate through all the controls that may be selected (ie: showing their selection handles) in a forms design view window. I have managed...
2
by: eight02645999 | last post by:
hi, i am using python 2.1. Can i use the code below to simulate the enumerate() function in 2.3? If not, how to simulate in 2.1? thanks from __future__ import generators def...
21
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.