Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach (Control c in control.Controls) is choking on the "in" 1

Status
Not open for further replies.

Gzk2010

Programmer
Aug 17, 2010
48
US
foreach (Control c in control.Controls) is choking on the "in"

I have been trying to get c# code behind for an asp.net web app to simply clear text boxes and people have given me good snippits like:

protected void ClearTextBoxes(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
((TextBox)c).Text = "";
if (c is CheckBox)
((CheckBox)c).Checked = false;
}
}



...but in all of them, this and similair snippets like ...
public void ClearTextBoxes(WebControl Parent)
{
foreach (WebControl child in Parent.Controls)
{
if (typeof(child) is TextBox)
{
((TextBox)child).Text = String.Empty;
}
else if (typeof(child) is IContainer)
{
ClearTextBoxes(child);
}
}
}


...my debugger hits the key word "in" and steps over the code in the foreach. AnyOne know why?
 
That would usually mean that the Controls collection of the top level Control c object is null or empty. In the debugger, (use the immediate window if you need to), verify the Control being passed to the method and verify the Controls collection of this control.

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top