How to Step through the controls in the Forms collection

  • Thread starter Thread starter Dave Adler
  • Start date Start date
D

Dave Adler

Can anyone give me an example in vb.net of how to step through all of the
controls in the current webform controls collection to test the Name value
of each control to look for controls that start with a specific name value
using a "for each/next" construct?

Thanks in advance

David
 
dim myForm as System.Web.UI.Control
dim myControl as System.Web.UI.Control

For Each myControl in MyForm
...
Next

HTHs

Daniel Walzenbach
 
this only gets the top level, you need to recurse

ArrayList list = FindControlsThatStartWithTest(Page);

ArrayList FindControlsThatStartWithTest (Control base)
{
ArrayList list = new ArrayList();
foreach (Control ctl in base.Controls)
{
if (ctl.Name.StartsWith("Test"))
list.Add(ctl);
list.AddRange(FindControlsThatStartWithTest(ctl));
}
}

bruce (sqlwork.com)
 
Back
Top