• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 63
  • Last Modified:

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 asp.net page are Layer1GridView, Layer2GridView, etc.  I need to access these gridview in page load in vb.net 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.
0
rckrch
Asked:
rckrch
  • 3
1 Solution
 
Ryan ChongCommented:
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)
        Else
            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.
0
 
rckrchAuthor Commented:
It returns the correct value for GridName.  Still is nothing
0
 
rckrchAuthor Commented:
This page is on a mater page.  Could this be the source of my problem?
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now