Thursday, March 29, 2012

Loop all controls in form

I have a method which list out all control's IDs but at the form level only, it will not load controls if it were from a Web User Control inside the form.

I can actually use Page.Form.Controls to go a level lower to scan control's controls, but it there a recursive way to get all Controls IDs existing in the form, or is there a better way to do it?

Cheers.

protectedvoid ListAllControls()

{

IEnumerator en = Page.Form.Controls.GetEnumerator();

StringBuilder sb =newStringBuilder();

while (en.MoveNext())

{

Control ctrl = (Control)en.Current;

if (ctrl.ID ==null)continue;

sb.Append("<br>" + ctrl.ID);

}

lblMessage.Text = sb.ToString();

}

The last 2 code samples inthis article show how to recursively iterate through all the controls.

0 comments:

Post a Comment