How to correctly find an array control ?

HI

I have created in  Public Sub InstantiateIn(ByVal container As System.Web.UI.Control)  event an array like this:
Dim lblcontrols(15) As Label

and I and I have instantiated from Item_DataBinding event, as follows:
CType(ph.FindControl("lblcontrols(1)"), Label).Text = "some text"

but I got an error about to "non referenced control"

dimensionavAsked:
Who is Participating?
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.

 
ZhaolaiCommented:
When you declare the array in one event, it has a scope for just that event ONLY. Any other event can't see it.
 
 You need to declare the array in the class level, so that all the events (and subs, functions)  in that class can reference it, like this:
 

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Dim lblcontrols(15) As Label
    
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control)

    End Sub
End Class

Open in new window

0
 
dimensionavAuthor Commented:
I believe this is more complex than I thought, I have attached some code, please notice that the 4 groups of commented sections were working but the idea of my question was because I wanted to optimize that code.

Regards!
<%@ Page Language="VB" MasterPageFile="menu_principal.master" Title="Untitled Page" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="system.Data.OleDb" %>

<script runat="server">
    Public Class CustomTemplate
        Implements System.Web.UI.ITemplate

        Dim templateType As ListItemType

        Sub New(ByVal type As ListItemType)
            templateType = type
        End Sub
        
        
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
          Implements System.Web.UI.ITemplate.InstantiateIn

            Dim ph As New PlaceHolder()
            'Dim campo1, campo2, campo3, campo4, campo5, campo6, campo7, campo8, campo9, campo10, _
            '    campo11, campo12, campo13, campo14, campo15 As New Label()
            'Dim valor1, valor2, valor3, valor4, valor5, valor6, valor7, valor8, valor9, valor10, _
            '    valor11, valor12, valor13, valor14, valor15 As New Label()
            
            Dim campos(15) As Label
            Dim valores(15) As Label

            Dim lblcampo As Label
            Dim lblvalor As Label
            For i As Short = 1 To 15
                lblcampo = New Label
                lblvalor = New Label
                lblcampo.ID = "campo" & i.ToString
                lblvalor.ID = "valor" & i.ToString
                campos(i) = lblcampo
                valores(i) = lblvalor
            Next
            
            'campo1.ID = "campo1"
            'campo2.ID = "campo2"
            'campo3.ID = "campo3"
            'campo4.ID = "campo4"
            'campo5.ID = "campo5"
            'campo6.ID = "campo6"
            'campo7.ID = "campo7"
            'campo8.ID = "campo8"
            'campo9.ID = "campo9"
            'campo10.ID = "campo10"
            'campo11.ID = "campo11"
            'campo12.ID = "campo12"
            'campo13.ID = "campo13"
            'campo14.ID = "campo14"
            'campo15.ID = "campo15"
            'valor1.ID = "valor1"
            'valor2.ID = "valor2"
            'valor3.ID = "valor3"
            'valor4.ID = "valor4"
            'valor5.ID = "valor5"
            'valor6.ID = "valor6"
            'valor7.ID = "valor7"
            'valor8.ID = "valor8"
            'valor9.ID = "valor9"
            'valor10.ID = "valor10"
            'valor11.ID = "valor11"
            'valor12.ID = "valor12"
            'valor13.ID = "valor13"
            'valor14.ID = "valor14"
            'valor15.ID = "valor15"

            Select Case (templateType)
                Case ListItemType.Item
                    ph.Controls.Add(New LiteralControl( _
                      "<table width=""270"" border=""0"" cellspacing=""0"" cellpadding=""0"">" & _
                      "<tr><td height=""150"" align=""center"" valign=""middle"" bgcolor=""#CCCCCC""><img src=""imagenes/3.png"" width=""270"" height=""120"" /></td>" & _
                      "</tr>" & _
                      "<tr>" & _
                        "<td height=""80"" align=""center"" valign=""top"" bgcolor=""909090"">" & _
                        "<table width=""270"" border=""0"" cellspacing=""0"" cellpadding=""0"" bgcolor=""#909090"">" & _
                          "<tr>" & _
                            "<td width=""15"" height=""30"">&nbsp;</td>" & _
                            "<td height=""30"" align=""right"" valign=""middle"" class=""inicio""><a href=""#"" class=""ficha""> Ver Galería</a></td>" & _
                            "<td width=""18"" height=""30"">&nbsp;</td>" & _
                          "</tr>" & _
                          "<tr>" & _
                            "<td width=""15"" height=""50"">&nbsp;</td>" & _
                            "<td align=""right"" valign=""top"" class=""categorias"">" & _
                              "<table width=""100%"" border=""0"">" & _
                                "<tr>"))

                        
                    ph.Controls.Add(New LiteralControl("<tr><td width=""30"" align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(1))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(1))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(2))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(2))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(3))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(3))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(4))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(4))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(5))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(5))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(6))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(6))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(7))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(7))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(8))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(8))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(9))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(9))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(10))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(10))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(11))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(11))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(12))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(12))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(13))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(13))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(14))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(14))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    ph.Controls.Add(campos(15))
                    ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    ph.Controls.Add(valores(15))
                    ph.Controls.Add(New LiteralControl("</td></tr>"))

                    
                    
                    'ph.Controls.Add(New LiteralControl("<tr><td width=""30"" align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo1)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor1)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo2)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor2)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo3)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor3)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo4)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor4)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo5)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor5)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo6)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor6)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo7)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor7)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo8)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor8)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo9)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor9)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo10)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor10)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo11)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor11)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo12)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor12)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo13)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor13)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo14)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor14)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))

                    'ph.Controls.Add(New LiteralControl("<tr><td align=""right"" class=""categorias"">"))
                    'ph.Controls.Add(campo15)
                    'ph.Controls.Add(New LiteralControl("</td><td width=""13"">&nbsp;</td><td align=""left"" class=""info"">"))
                    'ph.Controls.Add(valor15)
                    'ph.Controls.Add(New LiteralControl("</td></tr>"))
                    
                    

                    ph.Controls.Add(New LiteralControl( _
                        "</table>" & _
                              "<br /></td>" & _
                            "<td width=""18"">&nbsp;</td>" & _
                          "</tr>" & _
                          "<tr>" & _
                            "<td height=""10"">&nbsp;</td>" & _
                            "<td height=""30"" align=""right"" valign=""middle"" class=""info""><a href=""#"" class=""ficha"">Descarga ficha técnica</a></td>" & _
                            "<td>&nbsp;</td>" & _
                          "</tr>" & _
                        "</table>" & _
                        "</td>" & _
                      "</tr>" & _
                    "</table>"))
                       
                    AddHandler ph.DataBinding, New EventHandler(AddressOf Item_DataBinding)
            End Select

            container.Controls.Add(ph)
        End Sub
    End Class
        
    Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ph As PlaceHolder = CType(sender, PlaceHolder)
        Dim ri As DataListItem = CType(ph.NamingContainer, DataListItem)

        For i As Integer = 1 To 15
            Dim titulocampo As String = "CAMPO" & i.ToString
            Dim titulovalor As String = "VALOR" & i.ToString
            
            Dim campo As String = Convert.ToString(DataBinder.Eval(ri.DataItem, titulocampo))
            Dim valor As String = Convert.ToString(DataBinder.Eval(ri.DataItem, titulovalor))
                
            Dim ctrlcampo As String = "campos(" & i.ToString & ")"
            Dim ctrlvalor As String = "valores(" & i.ToString & ")"

            CType(ph.FindControl(ctrlcampo), Label).Text = campo
            CType(ph.FindControl(ctrlvalor), Label).Text = valor
        Next
            
    End Sub
        
    'Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Dim ph As PlaceHolder = CType(sender, PlaceHolder)
    '    Dim ri As DataListItem = CType(ph.NamingContainer, DataListItem)

    '    For i As Integer = 1 To 15
    '        Dim titulocampo As String = "CAMPO" & i.ToString
    '        Dim titulovalor As String = "VALOR" & i.ToString

    '        Dim campo As String = Convert.ToString(DataBinder.Eval(ri.DataItem, titulocampo))
    '        Dim valor As String = Convert.ToString(DataBinder.Eval(ri.DataItem, titulovalor))

    '        CType(ph.FindControl(titulocampo), Label).Text = campo
    '        CType(ph.FindControl(titulovalor), Label).Text = valor
    '    Next

    'End Sub

    
    
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        Dim conn As OleDbConnection ' Archivo
        Dim ComandoLectura As OleDbDataAdapter ' SQL
        Dim LineaSQL As String
            
        'Establecer conexion
        conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|demo.mdb;")           
        LineaSQL = "SELECT Secciones.SECCION, Categorias.CATEGORIA, ProductosUsados.PRECIO, ProductosUsados.MONEDA, ProductosUsados.IVA, " & _
                   "ProductosUsadosCampos.CAMPO1, ProductosUsados.VALOR1, ProductosUsadosCampos.CAMPO2, ProductosUsados.VALOR2, " & _
                   "ProductosUsadosCampos.CAMPO3, ProductosUsados.VALOR3, ProductosUsadosCampos.CAMPO4, ProductosUsados.VALOR4, " & _
                   "ProductosUsadosCampos.CAMPO5, ProductosUsados.VALOR5, ProductosUsadosCampos.CAMPO6, ProductosUsados.VALOR6, " & _
                   "ProductosUsadosCampos.CAMPO7, ProductosUsados.VALOR7, ProductosUsadosCampos.CAMPO8, ProductosUsados.VALOR8, " & _
                   "ProductosUsadosCampos.CAMPO9, ProductosUsados.VALOR9, ProductosUsadosCampos.CAMPO10, ProductosUsados.VALOR10, " & _
                   "ProductosUsadosCampos.CAMPO11, ProductosUsados.VALOR11, ProductosUsadosCampos.CAMPO12, ProductosUsados.VALOR12, " & _
                   "ProductosUsadosCampos.CAMPO13, ProductosUsados.VALOR13, ProductosUsadosCampos.CAMPO14, ProductosUsados.VALOR14, " & _
                   "ProductosUsadosCampos.CAMPO15, ProductosUsados.VALOR15 " & _
                   "FROM (Secciones INNER JOIN (Categorias INNER JOIN ProductosUsados ON Categorias.ID_CATEGORIA = ProductosUsados.ID_CATEGORIA) " & _
                   "ON Secciones.ID_SECCION = ProductosUsados.ID_SECCION) INNER JOIN ProductosUsadosCampos ON Secciones.ID_SECCION = ProductosUsadosCampos.ID_SECCION"

            
        ComandoLectura = New OleDbDataAdapter(LineaSQL, conn)
        Dim Tabla As DataTable = New DataTable

        'dlProductos.HeaderTemplate = New CustomTemplate(ListItemType.Header)
        dlProductos.ItemTemplate = New CustomTemplate(ListItemType.Item)
        'dlProductos.AlternatingItemTemplate = New CustomTemplate(ListItemType.AlternatingItem)
        'dlProductos.FooterTemplate = New CustomTemplate(ListItemType.Footer)
        ComandoLectura.Fill(Tabla)

        'Ordenar la estructura de datos
        dlProductos.DataSource = Tabla
        dlProductos.DataBind()

    End Sub


    </script>

<asp:Content ID="Content1" ContentPlaceHolderID="cphProductos" Runat="Server">
        <asp:datalist ID="dlProductos" runat="server" CellPadding="0" CellSpacing="12"
                      RepeatColumns="3" RepeatDirection="Horizontal" BorderWidth="0px" 
                      HorizontalAlign="Left" ShowFooter="False" ShowHeader="False">
        </asp:datalist>                    
</asp:Content>

Open in new window

0
 
ZhaolaiCommented:
Try this, which is the same as the one you commented out, because you are trying to find the labels with IDs.

    Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ph As PlaceHolder = CType(sender, PlaceHolder)
        Dim ri As DataListItem = CType(ph.NamingContainer, DataListItem)

        For i As Integer = 1 To 15
            Dim titulocampo As String = "CAMPO" & i.ToString
            Dim titulovalor As String = "VALOR" & i.ToString

            Dim campo As String = Convert.ToString(DataBinder.Eval(ri.DataItem, titulocampo))
            Dim valor As String = Convert.ToString(DataBinder.Eval(ri.DataItem, titulovalor))

            CType(ph.FindControl(titulocampo), Label).Text = campo
            CType(ph.FindControl(titulovalor), Label).Text = valor
        Next

    End Sub

Open in new window

0
 
dimensionavAuthor Commented:
It worked but I am a littlebit confused because ID´s of controls are small letters while the method is searching for capital letters.
0
 
ZhaolaiCommented:
I believe that the IDs are case insensitive.
0

Experts Exchange Solution brought to you by ConnectWise

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
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.

All Courses

From novice to tech pro — start learning today.