Array of form objects?

  • Thread starter Thread starter John Mason
  • Start date Start date
J

John Mason

Hi,

I have 5 separate placeholders within a web form. Each contain varying
amounts of textboxes and labels that were dynamically created and added.

When retrieving the various textbox id's and text values I use a
for..next loop to step through:

For a =..
CType(plcAttendance2.Controls(a), TextBox).ID
CType(plcAttendance2.Controls(a), TextBox).Text
Next

Could someone pls show me how I can dynamically refer to each
placeholder? Something like..

For b = 1 to 5
For a = ...
CType(plcAttendance.Controls(a), TextBox).ID
CType(plcAttendance.Controls(a), TextBox).Text
Next
Next

??

This would save me a lot of time with coding!

Any help much appreciated..
 
John Mason said:
Hi,

I have 5 separate placeholders within a web form. Each contain varying
amounts of textboxes and labels that were dynamically created and added.

When retrieving the various textbox id's and text values I use a
for..next loop to step through:

For a =..
CType(plcAttendance2.Controls(a), TextBox).ID
CType(plcAttendance2.Controls(a), TextBox).Text
Next

Could someone pls show me how I can dynamically refer to each
placeholder? Something like..

For b = 1 to 5
For a = ...
CType(plcAttendance.Controls(a), TextBox).ID
CType(plcAttendance.Controls(a), TextBox).Text
Next
Next

??

This would save me a lot of time with coding!

Any help much appreciated..


Use the typeof operator to programmatically identify the control type during
run-time, e.g.,

Dim cntrl As Control
For Each cntrl In plcAttendance1.Parent.Controls
If TypeOf cntrl Is PlaceHolder Then
Dim childcntrl As Control
For Each childcntrl In cntrl.Controls
If TypeOf childcntrl Is TextBox Then
Response.Write(CType(childcntrl, TextBox).ID)
Response.Write(CType(childcntrl, TextBox).Text)
End If
Next
End If
Next
 
Thanks for that. Is there a way of reiterating through a group of
placeholders? I have 5 placeholders named...

plcAttendance1
plcAttendance2
..
..

I would prefer to reiterate through all of them at once instead of
writing separate code for each.

Thanks!
 
If the placeholder controls are spread in different places on the page you
need a recursive function to find them. For example the following function
would recur through a collection of controls to find TextBox controls within
PlaceHolder controls:


private void FindMyControls(ControlCollection cntrlcol)
{
foreach (Control cntrl in cntrlcol)
{
if (cntrl is PlaceHolder) {
foreach(Control childControl in cntrl.Controls) {
Response.Write(((TextBox)childControl).ID);
}
}
else if (cntrl.HasControls())
{
FindMyControls(cntrl.Controls);
}
}
}

You can call this function as follows:
FindMyControls(Page.Controls);
 
Back
Top