This is a migrated thread and some comments may be shown as answers.

Finding Control that caused PostBack

1 Answer 411 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
topry
Top achievements
Rank 1
topry asked on 26 Feb 2009, 08:33 PM

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 
- or -
    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  
 
but none of them have worked -

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.


1 Answer, 1 is accepted

Sort by
0
topry
Top achievements
Rank 1
answered on 26 Feb 2009, 09:26 PM
I found the answer - posting it here in case someone else bumps their head up against this...
The issue was I needed to use MasterPage.Page.FindControl.

    Public Function GetPostBackControl(ByVal thePage As MasterPage) As Control  
        Dim myControl As Control = Nothing 
        Dim ctrlName As String = thePage.Page.Request.Params.[Get]("__EVENTTARGET")  
        If ((ctrlName IsNot Nothing) AndAlso (ctrlName <> String.Empty)) Then  
            myControl = thePage.Page.FindControl(ctrlName)  
        Else  
            For Each Item As String In thePage.Page.Request.Form  
                Dim cl As Control = thePage.Page.FindControl(Item)  
                If (TypeOf (cl) Is System.Web.UI.WebControls.Button) Then  
                    myControl = cl 
                End If  
            Next  
        End If  
        Return myControl  
    End Function  
 
 
Tags
General Discussions
Asked by
topry
Top achievements
Rank 1
Answers by
topry
Top achievements
Rank 1
Share this question
or