Is it possible to obtain the control object of a child control within a dynamically loaded usercontrol?
Structure:
Website has single master page containing a Telerik ScriptManager and AjaxManager as described in other postings.
User controls contain an AjaxProxyManager and one or more Telerik controls as well as standard controls. There is nothing specific/unique to the Telerik controls, this problem occurs regardless of the control type.
During page_init, required usercontrols are loaded onto the selected page within a placeholder.
Everything works properly- but I need to identify which control within the user controls caused the postback.
I am using a recursive method to search the controls collection within each control (see sample code below).
Following is within the Page_Init()
If Page.IsPostBack Then |
'get the name of the control that caused the postback |
Dim ctlName As String = Request.Form("__EVENTTARGET") |
'find the control |
Dim thisCtrl As Control = FindControlRecursively(ctlName, Page) |
End If |
I have tried mutliple recursive methods to find the control based upon the ID:
Public Shared Function FindControlRecursively(ByVal ControlID As String, ByVal ParentControl As Control) As Control |
Dim c As Control |
For Each c In ParentControl.Controls |
Debug.WriteLine(c.ClientID.ToString) |
If IsNothing(c.ID) = False AndAlso c.ID = ControlID Then |
Return c |
Else |
If c.HasControls Then |
' loop through this control collection as well, until we find what we are looking for. |
Dim foundcontrol As Control = FindControlRecursively(ControlID, c) |
If IsNothing(foundcontrol) Then |
Continue For |
Else |
Return foundcontrol |
End If |
End If |
End If |
Next |
' if we didn't find the control by now, we aren't going to. |
Return Nothing |
End Function |
Public Shared Function FindChildControl(ByVal start As Control, ByVal id As String) As Control |
If start IsNot Nothing Then |
Dim foundControl As Control |
foundControl = start.FindControl(id) |
If foundControl IsNot Nothing Then |
Return foundControl |
End If |
For Each c As Control In start.Controls |
foundControl = FindChildControl(c, id) |
If foundControl IsNot Nothing Then |
Return foundControl |
End If |
Next |
End If |
Return Nothing |
End Function |
An example of a control name from a user control that has been dynamically added (in this instance the control is a LinkButton) is:
"ctl00$ContentPlaceHolder1$ctl00$lnkMail" where "lnkMail" is the ID of the control and the containing parent controls are those that preceed it. Regardless of the methods used ( .FindControl ) or iterating and comparing .ID, .UniqueID, or .ClientID - none of those values come close to matching and .FindControl never finds it regardless of what I pass (the full name or a subset thereof), even if I identify the correct parent control and invoke .FoundControl on that object. When I iterate through the parent object the .ID values are NULL and the .UniqueID values are ctl00, ctl01, etc.
Using a single webform, .FindControl and either of the recursive functions work as expected. The problem appears to be related to dynamically loading the usercontrols and then trying to locate a child control within the placeholder.