FindControl Help! ddl inside usercontrol from another control using formview control

Hello EE!

I think I'm close to getting what I need but my code bombs and I believe my formview code is to blame and how I'm trying to locate the control I need.  On my page (lets call it page.aspx) I have two web user controls (lets call them jc.ascx and ec.ascx). In jc.ascx I have a formview control which has a _ModeChanging event. In this event I am trying to get a hold of a dropdownlist control which resides in my ec.ascx which is on the same page. However, I don't think I'm using findcontrol right to navigate to the control.

My page.aspx control structure is as follows:


    ' This is ec.ascx
      <..."ID="ClassControl1"...>    <---- Trying to find a DDL control in here (ddl control is returned through a public function)
    </... "ID="EmpClassChange"...>

    ' This is jc.ascx
      <..."ID="JobControl1"...>  <----- From a formview within here
    </... "ID="JobChange"...>


Both code attempts shown below result in a 'Object reference not set to an instance of an object.' error in my _ModeChanging event.

I thought maybe this would do it... but no luck!!
Any thoughts on what I might be doing wrong?? I appreciate the assistance in advance.

'1st code attempt
                Dim tbContainer As AjaxControlToolkit.TabContainer = DirectCast(Parent.FindControl("TabContainer1"), AjaxControlToolkit.TabContainer)
                Dim pnl As Panel = DirectCast(tbContainer.FindControl("COS").FindControl("COSUpdatePanel").FindControl("EmpClassChange"), Panel)
                Dim ddl As DropDownList = CType(pnl.FindControl("ClassControl1"), modules_ClassControl).GetEmpClassDDL
                ddl.Enabled = False

'2nd code attempt
                'Dim ddl As DropDownList = CType(CType(Parent.FindControl("TabContainer1").FindControl("COS").FindControl("COSUpdatePanel").FindControl("EmpClassChange"), Panel).FindControl("ClassControl1"), modules_ClassControl).GetEmpClassDDL
                'ddl.Enabled = False

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
Try using a FindControlRecursive Function.  I provided two implementation of it.

Private Function FindControlsRecursive(ByVal controlID As String, ByVal controls As ControlCollection) As Control
        For Each c As Control In controls
            If c.ID = controlID Then
                Return c
            End If
            If c.HasControls Then
                Dim cTmp As Control = Me.FindControlsRecursive(controlID, c.Controls)
                If cTmp IsNot Nothing Then
                    Return cTmp
                End If
            End If
        Return Nothing

    End Function

    Private Function FindControlRecursive(ByVal ctrl As Control, ByVal id As String) As Control
        If ctrl.ID = id Then
            Return ctrl
        End If

        For Each ctrlChild As Control In ctrl.Controls
            Dim ctrlRecursive = FindControlRecursive(ctrlChild, id)
            If Not ctrlRecursive Is Nothing Then
                Return ctrlRecursive
            End If

        Return Nothing
    End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alfred A.Commented:
Example use of the functions above (2nd function)

Dim ddl As DropDownList = CType(FindControlRecursive(TabContainer1, "EmpClassDDL"),DropDownList)
sk1922Author Commented:
Sorry for the delay in getting back on this.  That worked well for me!!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.