Finding Gridview

I have been trying to find a grdiview control dynamically with variable gridview names.  I have a page with 3 gridviews each has a unique id determined by an integer in string name of the gridview.  I am trying to find this grid view with the following code.

The names of the gridviews in the page are Layer1GridView, Layer2GridView, etc.  I need to access these gridview in page load in to assign values to the columns.  The below code is not working.  Can someone help me with this?

Thanks in advance.

Dim GridName As String
GridName = "Layer" & LayerNumber & "GridView"
Dim NewGrid As WebControls.GridView = DirectCast(Me.FindControl(GridName), WebControls.GridView)

Going through this procedure the NewGrid is nothing.
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
your code is working well.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim LayerNumber As Integer 
        Dim GridName As String

        LayerNumber = 3

        GridName = "Layer" & LayerNumber & "GridView"
        Dim NewGrid As WebControls.GridView = DirectCast(Me.FindControl(GridName), WebControls.GridView)
        If NewGrid Is Nothing Then
            Response.Write("Cannot find control with ID: " & GridName)
            Response.Write("Found control:" & NewGrid.ClientID)
        End If
    End Sub

Open in new window

Going through this procedure the NewGrid is nothing.
make sure the GridName returned the correct value when doing the comparison above.

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
rckrchAuthor Commented:
It returns the correct value for GridName.  Still is nothing
rckrchAuthor Commented:
This page is on a mater page.  Could this be the source of my problem?
rckrchAuthor Commented:
That was the problem - since this page is on a master page I have us DirectCast(Me.Master.FindControl("MainContent").FindControl....)

Thanks for the help.
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.