Nurasto.com Website

My digital scrapbook

Find All Controls ID in ASP.NET WebForms

on under Software and Web DevelopmentBe the first to comment

Every ASP.NET developer know to find and cast a control but sometimes finding the control name is PITA. I face this kind of problem when still actively programming C# in .NET 2.0 era. So, I have an idea to write junk code in asp.net code behind and throw up into output window rather than putting breakpoints and looking for control ID.

private void ControlExplorer(System.Web.UI.ControlCollection control)
{
	if (control.Count > 0)
	{
		foreach (System.Web.UI.Control currentControl in control)
		{
			System.Diagnostics.Debug.WriteLine(currentControl.GetType().FullName);
			System.Diagnostics.Debug.WriteLine(currentControl.ID);

			this.ControlExplorer(currentControl.Controls);
		}
	}
}

You can call the method inside an control event, choose the target (e.g wizardForm.Controls) and it will print out everything you want. You’re welcome.