In 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?
-
11-09-2019 - |
Question
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
Solution
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");
OTHER TIPS
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow