473,763 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aug 4 '06 #1
3 1688
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.HasContro ls() 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(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);
}
}
}
Aug 4 '06 #2
you need to add the controls on postback

-- bruce (sqlwork.com)
"Mike Collins" <Mi*********@di scussions.micro soft.comwrote in message
news:C4******** *************** ***********@mic rosoft.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(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);
}
}
}

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*********@di scussions.micro soft.comwrote in message
news:C4******** *************** ***********@mic rosoft.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(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);
}
}
}


Aug 4 '06 #4

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

Similar topics

1
4565
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 (Control.BringToFront()) and being able to place it back later in the original order. Any suggestions are highly appreciated! Thank you Andrey
7
4547
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" MS_POSITIONING="GridLayout" class="Body" runat="server"> And in my code-behind I want to look for the leftMargin
0
924
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
6971
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 same. How to perform the above in c#? Is PInvoke the only way to do it or Is there some inherent c# way?
3
1674
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 getting the hang of it I'm afraid. Right now I'm parsing text file but that's a pain, not very elegant, and I'm not sure it'll give me all the info I need. Right now I'm using VB.Net as the back end but I'll take C#.Net code in a pinch. :) Tanx in...
1
1813
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. I posted a very similar item to python-dev, but they said to post it here. Tutor also said that if anything is to be considered as PEP worthy, it should be run through here first. So...here goes. Whenever I use enumerate, I am doing so...
1
2742
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 to capture the active form and can iterate through all the controls on the form but I want to be able to minimise the list to only the controls with the selection handles are identified. Any help would be greatly appreciated.
2
2445
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 enumerate(sequence): index = 0 for item in sequence: yield index, item
21
2331
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 than in the current enumerate, tightens up code in many cases, and seems that it would break no
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.