FindControl in gridview on web page which is on master page.

FindControl will not work when searching for control in gridview which in on master page.  Neither one of the 'Dim ....' options listed below will work.  This is on a master page, but it is in the RowDataBound routine where I am trying to access the control.

Protected Sub SchLayer1GridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

Dim NewNameDrpDn As DropDownList = CType(e.Row.FindControl("DrpDnName1"), DropDownList)

Dim NewNameDrpDn As DropDownList = DirectCast(e.Row.FindControl("DrpDnName1"), DropDownList)

End Sub

Any help would be appreciated.

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.

Chinmay PatelChief Technical NinjaCommented:
FindControl only works if the child control is directly container within a container. In your case there is a hierarchy of containers. This classic problem is solved with the following snippet.

Private Function FindControlRecursive(
    ByVal rootControl As Control, ByVal controlID As String) As Control

    If rootControl.ID = controlID Then
        Return rootControl
    End If

    For Each controlToSearch As Control In rootControl.Controls
        Dim controlToReturn As Control = 
            FindControlRecursive(controlToSearch, controlID)
        If controlToReturn IsNot Nothing Then
            Return controlToReturn
        End If
    Return Nothing
End Function

Open in new window

Source MSDN Link:

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
Chinmay PatelChief Technical NinjaCommented:
Requesting a close as Author has endorsed the comment and technically this is the correct answer.
I am more than happy to explain further or correct my answer if author / moderator objects.
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

From novice to tech pro — start learning today.