Recursion Method Problem

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Hello,

I have a recursive method that looks through an ASP.Net page to find a
control by ID. My problem is after it finds the control, breaks from the
loop and returns the control, it goes to my recursive line (if
control.HasControls call the findctl method). I think the problem lies in
that the method returns a value. Is this the case? Any ideas?

Thanks

private Control FindCtl(Control oControl, string controlID)
{
Control control = new Control();
foreach (Control ctl in oControl.Controls)
{

if (ctl.ID == controlID)
{
control = ctl;
break;
}

if (ctl.HasControls())
{
FindCtl(ctl, controlName);
}
}
return control;
}
 
Mark,

It finds the control however after it breaks out of the loop and then hits
return control line it goes up to FindCtl(ctl, controlName); line. I do not
know why this is happening?

Thanks
 
I would change it to something like:

if (ctl.HasControls())
{
control = FindCtl(ctl, controlName);

if (control != null)
break;
}
 
Back
Top