views:

425

answers:

2

n ASP.NET how do I find the Control ID of a TextBox that is nested within a DetailsView which is then nested in an AJAX UpdatePanel control ?

The heirachy is: UpdatePanel1 -> dvContentDetail (DetailsView Control) -> TextBox2

I have tried something like the following but just says that the object isn't found:

UpdatePanel1.FindControl("dvContentDetail").FindControl("TextBox2").ClientID
A: 

You could try something like the code below. But if you know the Hierarchy is not going to change it would be better to do a series of "FindControl" calls. To discover the correct hierarchy, Debug the app and search through the Control Hierarchy.

public static T FindControlRecursiveInternal<T>(Control startingControl, string controlToFindID) where T : Control
{
 if (startingControl == null || String.IsNullOrEmpty(controlToFindID))
  return (T)null;

 Control foundControl = startingControl.FindControl(controlToFindID);
 if (foundControl == null)
 {
  foreach (Control innerControl in startingControl.Controls)
  {
   foundControl = FindControlRecursiveInternal<T>(innerControl, controlToFindID);
   if (foundControl != null)
    break;
  }
 }

 return (T)foundControl;
}
Aaron Hoffman
+1  A: 

There is no need to find control from updatepanel, because these controls directly available, so you code will be like this...

TextBox TextBox2 = (TextBox)dvContentDetail.FindControl("TextBox2");
Muhammad Akhtar
This didn't work - just got the same error message saying "Object reference not set to an instance of an object".
cyberbobcat
This did work once I had put the code in the right place ! - ie. after the control had been databound. - Thanks.
cyberbobcat