Monday, March 26, 2012

Loop Through Control Collection

Hey everyone,

I'm getting stuck on a wierd problem. I've got an <asp:wizard> on my page, that contains several steps. At the end of the wizard, I would like to provide a summary of the results based on the options chosen throughout the wizard.

My wizard is full of controls, so I wont post all that code here and make a mess, but I'm trying to loop through the control collection of the wizard, and get the controls name, and it's value.

I've looped through control collections on pages before using panels, or the forms controll collection without issues, but I'm running into trouble getting the controls out of the wizard collection.

I'm looping like this...maybe it's a bad way.

ForEach ctrlAs ControlIn Wizard1.Controls

...code here

Next

if you know the names of all the controls then you can code for each control to pull the value

ex.

labeloutput.text = ctype(findcontrol("txttextbox"), textbox).text


I know the name of each control by it's ID, what I wanted to avoid was having to hard code each controls name.

I could just do

label.text = Control1.text & control2.text & dropdownlist1.selectedvalue.tostring...etc

I didnt want to have to go through all that.


Keep in mind also that FindControl is not recursive, so you will have to call find control from the container. For example, MyWizard.Controls.FindControl(...etc).

Freakyuno wrote:

Hey everyone,

I'm getting stuck on a wierd problem. I've got an <asp:wizard> on my page, that contains several steps. At the end of the wizard, I would like to provide a summary of the results based on the options chosen throughout the wizard.

My wizard is full of controls, so I wont post all that code here and make a mess, but I'm trying to loop through the control collection of the wizard, and get the controls name, and it's value.

I've looped through control collections on pages before using panels, or the forms controll collection without issues, but I'm running into trouble getting the controls out of the wizard collection.

I'm looping like this...maybe it's a bad way.

ForEach ctrlAs ControlIn Wizard1.Controls

...code here

Next

Recursion would be your friend in your case

In your code

Dim AllWizardControlIDs As New System.Collections.StringCollection
WalkControls(AllWizardControlIDs, Wizard1)
' the StringCollection "AllWizardControlsIDs" now has all the ID's of all controls in it

Have this Sub somewhere

Private Sub WalkControls(ByRef i_List As System.Collections.StringCollection, ByVal i_Parent As Control)
For Each ChildControl As Control In i_Parent
If TypeOf ChildControl Is TextBox Then
i_List.Add(ChildControl.ID)
ElseIf TypeOf ChildControl Is DropDownList Then
i_List.Add(ChildControl.ID)
'***** Etc etc for each control type *****
End If
WalkControls(i_List, ChildControl)
Next
End Sub


Thanks for your Response Everyone.

MorningZ, your code is giving me some problems. I about half way understand what your trying to do.

I am using asp.net 2.0. Not sure if that makes a difference. But I had to reference System.Collections.Specialized.StringCollection instead of the way you had it, also, when I load your code, I get an error saying i_parrent isnt a collection. I tried changing it so that it passed controlls instead of controll but that didnt help.

I'm also not sure when your loop terminates, if your calling the sub from within itself. Can you help?

Thanks again.


Sorry it wasnt perfect code, i was just wirting it off the top of my head on the reply, and the fact its 2.0 wont matter, there's nothing 1.1 or 2.0 specific other than using a StringCollection rather than the ArrayList

For your error, sound like you don't have the params in the right order

Here's some exact code i am using
http://www.aspmessageboard.com/forum/showMessage.asp?F=36&M=756202&P=1

And to answer the "when does it end" question... when the i_Parent passed in has no children, the sub does nothing, so it'll back off that recursive call and go onto the next

Recursion is tough to grasp, but once you do, it's a very powerful ally :)


MorningZ, thats exactaly what I needed, between the two I managed to get a list of the controls on the page.

Thanks for the help. :)

0 comments:

Post a Comment