Cannot create object from string representation in a custom control

Hi,
I'm building a custom control and I add a propertiy to select one item from the control's collection.
It works well in the designer but when I try to run a project I get the message
Cannot create an object of type 'HQ.FltColumn' from its string representation 'FltColumn2' for the 'SortCol' property
The property is SortCol where I want to select the column who will be use for the sorting.

Here's the code for the control:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.drawing


<PersistChildren(False), ParseChildren(True, "Columns"), Designer(GetType(HQFilterDesigner))> _
Public Class HQFilter
    Inherits WebControl


#Region "Init"

    Private WithEvents grd As New Infragistics.WebUI.UltraWebGrid.UltraWebGrid
    Private _FltColumns As ColCollection = New ColCollection
    Private _Sortcol As FltColumn
    Private _indexcol As FltColumn
    Private imgSearch, imgSearchH, imgBack, imgExpand, imgExpandH, imgColl, imgCollH As String
    Private ds As DataSet



    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then

        End If



    End Sub
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)


        'A l'initialisation de la page on enregistre les scripts et les images

        Dim scriptLocation As String = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.HQFilter.js")
        Page.ClientScript.RegisterClientScriptInclude("HQ.HQFilter.js", scriptLocation)

        imgSearch = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.search.jpg")
        imgSearchH = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.search_h.jpg")
        imgBack = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.HitList_back.png")
        imgExpand = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.Expand.png")
        imgExpandH = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.Expand_h.png")
        imgColl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.collapse.png")
        imgCollH = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "HQ.collapse_h.png")



        MyBase.OnInit(e)
        EnsureChildControls()

    End Sub

#End Region

#Region "Hidden properties"

    <Browsable(False)> _
        Public Overrides Property AccessKey() As String
        Get
            Return MyBase.AccessKey
        End Get
        Set(ByVal value As String)
            MyBase.AccessKey = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property BackColor() As System.Drawing.Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BackColor = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property BorderColor() As System.Drawing.Color
        Get
            Return MyBase.BorderColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BorderColor = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property BorderStyle() As System.Web.UI.WebControls.BorderStyle
        Get
            Return MyBase.BorderStyle
        End Get
        Set(ByVal value As System.Web.UI.WebControls.BorderStyle)
            MyBase.BorderStyle = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property BorderWidth() As System.Web.UI.WebControls.Unit
        Get
            Return MyBase.BorderWidth
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            MyBase.BorderWidth = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property CssClass() As String
        Get
            Return MyBase.CssClass
        End Get
        Set(ByVal value As String)
            MyBase.CssClass = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property EnableTheming() As Boolean
        Get
            Return MyBase.EnableTheming
        End Get
        Set(ByVal value As Boolean)
            MyBase.EnableTheming = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides ReadOnly Property Font() As System.Web.UI.WebControls.FontInfo
        Get
            Return MyBase.Font
        End Get
    End Property
    <Browsable(False)> _
    Public Overrides Property ForeColor() As System.Drawing.Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.ForeColor = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property Height() As System.Web.UI.WebControls.Unit
        Get
            Return MyBase.Height
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            MyBase.Height = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property SkinID() As String
        Get
            Return MyBase.SkinID
        End Get
        Set(ByVal value As String)
            MyBase.SkinID = value
        End Set
    End Property
    <Browsable(False)> _
    Public Overrides Property Width() As System.Web.UI.WebControls.Unit
        Get
            Return MyBase.Width
        End Get
        Set(ByVal value As System.Web.UI.WebControls.Unit)
            MyBase.Width = value
        End Set
    End Property

#End Region

#Region "Properties"

    Public Enum Expansion
        ScrollBar = 0
        Expand = 1
    End Enum

    <PersistenceMode(PersistenceMode.InnerDefaultProperty), _
      DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property [Columns]() As ColCollection
        Get
            Return _FltColumns
        End Get
    End Property
    <Description("Colonne qui sera utilisée pour le tri initial de la liste")> _
    Public Property SortCol() As FltColumn
        Get
            Return _Sortcol
        End Get
        Set(ByVal value As FltColumn)
            _Sortcol = value
        End Set
    End Property
    <Description("Colonne qui sera utilisée pour la sélection des enregistrements")> _
   Public Property IndexCol() As FltColumn
        Get
            Return _indexcol
        End Get
        Set(ByVal value As FltColumn)
            _indexcol = value
        End Set
    End Property
    <Description("Largeur de la grille en expansion")> _
       Property ExpandSize() As Integer
        Get
            Dim o As Object = ViewState("ExpandSize")
            If o Is Nothing Then
                Return 200
            End If
            Return CType(ViewState("ExpandSize"), Integer)
        End Get
        Set(ByVal Value As Integer)
            ViewState("ExpandSize") = Value

        End Set
    End Property
    <Description("Hauteur du controle de filtre")> _
    Property FilterHeight() As Integer
        Get
            Dim o As Object = ViewState("FFHeight")
            If o Is Nothing Then
                Return 200
            End If
            Return CType(ViewState("FFHeight"), Integer)
        End Get
        Set(ByVal Value As Integer)
            ViewState("FFHeight") = Value

        End Set
    End Property
    <Description("Largeur du controle de filtre")> _
    Property FilterWidth() As Integer
        Get
            Dim o As Object = ViewState("FFWidth")
            If o Is Nothing Then
                Return 200
            End If
            Return CType(ViewState("FFWidth"), Integer)
        End Get
        Set(ByVal Value As Integer)
            ViewState("FFWidth") = Value

        End Set
    End Property

    <Description("Affichage de la barre déroulante horizontale")> _
   Property GridNavigation() As Expansion
        Get
            Dim o As Object = ViewState("GridNavigation")
            If o Is Nothing Then
                Return Expansion.Expand
            End If
            Return CType(ViewState("GridNavigation"), Expansion)
        End Get
        Set(ByVal Value As Expansion)
            ViewState("GridNavigation") = Value

        End Set
    End Property

    <Browsable(False)> Property [Datasource]() As DataSet

        Get
            Dim dsData As DataSet = CType(ViewState("FFDatasource"), DataSet)
            Return dsData
        End Get
        Set(ByVal Value As DataSet)
            ViewState("FFDatasource") = Value
        End Set

    End Property

#End Region

#Region "Constructors"


    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)


     


    End Sub
    Private Sub CreateCol()
        grd.Height = Me.FilterHeight - 45
        grd.Width = Me.FilterWidth



        Dim cc As Infragistics.WebUI.UltraWebGrid.UltraGridColumn

        If Not IsNothing(Me.Datasource) Then
            If Me.Datasource.GetType Is GetType(DataSet) Then
                Dim ds As DataSet = Me.Datasource


                For Each fltc As FltColumn In Me.Columns
                    For Each c As DataColumn In ds.Tables(0).Columns
                        If fltc.Key = c.ColumnName Then
                            If IsNothing(grd.Columns.FromKey(fltc.Key)) Then
                                cc = New Infragistics.WebUI.UltraWebGrid.UltraGridColumn
                                grd.Columns.Add(cc)
                            Else
                                cc = grd.Columns.FromKey(fltc.Key)
                            End If

                            With cc
                                If Not IsNothing(Me.SortCol) Then
                                    If Me.SortCol.Key = fltc.Key Then
                                        .SortIndicator = Infragistics.WebUI.UltraWebGrid.SortIndicator.Ascending
                                    End If
                                End If
                               
                                .Key = fltc.Key
                                .Width = fltc.ColWidth
                                .Hidden = Not fltc.VisibleInFilter
                                .BaseColumnName = fltc.Key
                                .Header.Caption = fltc.Header
                                .Header.Style.HorizontalAlign = fltc.HeaderAlign
                                If fltc.HeaderAlign <> HorizontalAlign.Center Then
                                    .Header.Style.Padding.Left = Unit.Pixel(10)
                                End If
                                .CellStyle.HorizontalAlign = fltc.CellAlign
                                If fltc.CellAlign <> HorizontalAlign.Center Then
                                    .CellStyle.Padding.Left = Unit.Pixel(10)
                                End If

                            End With



                        End If
                    Next
                Next



            End If
        End If
    End Sub
    Private Sub grd_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.LayoutEventArgs) Handles grd.InitializeLayout
       
    End Sub
    Private Sub grd_InitializeDataSource(ByVal sender As System.Object, ByVal e As Infragistics.WebUI.UltraWebGrid.UltraGridEventArgs) Handles grd.InitializeDataSource

        CreateCol()
        grd.DataSource = Me.Datasource
        grd.DataBind()

     
    End Sub

    Protected Overloads Overrides Sub CreateChildControls()



        'Bouton de recherche
        Dim imgS As New HtmlControls.HtmlImage
        With imgS
            .Attributes.Add("OnMouseOver", "this.src = '" & imgSearchH & "'")
            .Attributes.Add("OnMouseOut", "this.src = '" & imgSearch & "'")
            .Attributes.Add("OnClick", "OpenSearch();")
            .Src = imgSearch
            .Alt = "Ouvrir la fenêtre de recherche"
        End With

        'Cellule qui contient le bouton
        Dim c1Title As New TableCell
        With c1Title
            '.Style.Add("PADDING-LEFT", "10px")
            .Controls.Add(imgS)
        End With

        'Titre
        Dim lblTitle As New Label
        With lblTitle
            .Text = "Filtre de recherche"
            .Font.Name = "Tahoma"
            .Font.Size = 10
            .Font.Bold = True
        End With

        'Cellule qui contient le titre
        Dim c2Title As New TableCell
        With c2Title
            .Controls.Add(lblTitle)
            .HorizontalAlign = HorizontalAlign.Center
        End With

        'Bouton d'expansion
        Dim imgE As New HtmlControls.HtmlImage
        With imgE
            .Attributes.Add("OnMouseOver", "this.src = '" & imgExpandH & "'")
            .Attributes.Add("OnMouseOut", "this.src = '" & imgExpand & "'")
            .Attributes.Add("OnClick", "expandGrd('" & Me.ID & "','" & _
                 Me.ExpandSize & "','" & Me.FilterWidth & "','" & Me.FilterHeight & _
                 "','" & imgExpand & "','" & imgExpandH & "','" & imgColl & "','" & imgCollH & "')")
            .Src = imgExpand
            .Alt = "Elargir la grille"
            .ID = Me.ID & "_img"
        End With

        'Cellule qui contient le bouton expand
        Dim c3Title As New TableCell
        With c3Title
            '.Style.Add("PADDING-LEFT", "10px")
            .HorizontalAlign = HorizontalAlign.Right
            If Me.GridNavigation = Expansion.Expand Then
                .Controls.Add(imgE)
            End If

        End With

        'Rangée du titre et bouton de recherche
        Dim rTitle As New TableRow
        With rTitle
            .BorderWidth = New Unit(0)
            .HorizontalAlign = HorizontalAlign.Left
            .Height = New Unit(20)
            .BackColor = Color.FromName("#aeada0")
            .Cells.Add(c1Title)
            .Cells.Add(c2Title)
            .Cells.Add(c3Title)
        End With




        'Grille de sélection
        With grd
            .Browser = Infragistics.WebUI.UltraWebGrid.BrowserLevel.Xml
            .BackColor = Color.FromArgb(149, 178, 195)
            .ID = Me.ID & "_grd"
            .BorderStyle = WebControls.BorderStyle.Solid
            .BorderWidth = 1
            .BorderColor = Color.Black




            With .DisplayLayout


                If Me.GridNavigation = Expansion.ScrollBar Then
                    .ScrollBar = Infragistics.WebUI.UltraWebGrid.ScrollBar.Auto
                    .ScrollBarView = Infragistics.WebUI.UltraWebGrid.ScrollBarView.Both
                Else
                    .ScrollBar = Infragistics.WebUI.UltraWebGrid.ScrollBar.Auto
                    .ScrollBarView = Infragistics.WebUI.UltraWebGrid.ScrollBarView.Vertical
                End If
                .AutoGenerateColumns = False
                .TableLayout = Infragistics.WebUI.UltraWebGrid.TableLayout.Auto
                .RowSelectorsDefault = Infragistics.WebUI.UltraWebGrid.RowSelectors.No
                .AllowUpdateDefault = Infragistics.WebUI.UltraWebGrid.AllowUpdate.No
                .AllowSortingDefault = Infragistics.WebUI.UltraWebGrid.AllowSorting.OnClient
                .HeaderClickActionDefault = Infragistics.WebUI.UltraWebGrid.HeaderClickAction.SortSingle

                .LoadOnDemand = Infragistics.WebUI.UltraWebGrid.LoadOnDemand.Xml
                .Pager.AllowPaging = True
                .Pager.PageSize = 50
                .RowsRange = 50
                .Pager.StyleMode = Infragistics.WebUI.UltraWebGrid.PagerStyleMode.ComboBox

                .CellClickActionDefault = Infragistics.WebUI.UltraWebGrid.CellClickAction.RowSelect
                .RowStyleDefault.BackColor = Drawing.Color.FromArgb(238, 249, 253)
                .RowAlternateStyleDefault.BackColor = Drawing.Color.FromArgb(215, 236, 244)
                .SelectedRowStyleDefault.BackColor = Drawing.Color.FromArgb(255, 204, 51)
                .RowStyleDefault.Font.Size = FontUnit.Point(10)
                .Grid.Font.Name = "Tahoma"
                .Grid.Font.Size = FontUnit.Point(10)
                .ClientSideEvents.DblClickHandler = "LineClick"
                .RowStyleDefault.Height = 20
                .StationaryMargins = Infragistics.WebUI.UltraWebGrid.StationaryMargins.Header

                With .HeaderStyleDefault
                    .Font.Name = "Tahoma"
                    .Font.Size = System.Web.UI.WebControls.FontUnit.Point(10)
                    .Font.Bold = True
                    .BackgroundImage = imgBack
                End With
            End With
        End With
       



        ''Panel qui contient la grille
        'Dim pnlGrd As New Panel
        'With pnlGrd
        '    .ID = "pnlGrd"
        '    .Controls.Add(grd)
        '    .Style.Add("border-bottom", "black 10px solid")
        '    .Style.Add("border-right", "black 1px solid")
        '    .Style.Add("border-left", "black 1px solid")
        '    .Height = Me.FilterHeight - 20
        '    .Width = Me.FilterWidth
        '    .ScrollBars = ScrollBars.Auto
        'End With


        ''Option d'expansion pour le panel
        'Dim exp As New AjaxControlToolkit.CollapsiblePanelExtender
        'With exp
        '    .AutoCollapse = True
        '    .AutoExpand = True
        '    .Collapsed = True
        '    .CollapsedSize = Me.FilterWidth
        '    .ExpandDirection = AjaxControlToolkit.CollapsiblePanelExpandDirection.Horizontal
        '    .ExpandedSize = 400
        '    .TargetControlID = "pnlGrd"
        'End With


        'Table qui contient le titre
        Dim tbControl As New Table
        With tbControl
            .CellPadding = 0
            .CellSpacing = 0
            .Width = Me.FilterWidth
            .Rows.Add(rTitle)
            .Style.Add("border-top", "black 1px solid")
            .Style.Add("border-right", "black 1px solid")
            .Style.Add("border-left", "black 1px solid")
        End With



        Controls.Add(tbControl)
        Controls.Add(grd)










    End Sub







#End Region

End Class



#Region "Object class"


'Définition des propriétés des colonnes

<DesignTimeVisible(False), ToolboxItem(False)> Public Class FltColumn
    Inherits Control
    Implements INamingContainer


    Dim _ID As String
    Dim _key As String
    Dim _visible As Boolean
    Dim _format As String
    Dim _header As String
    Dim _showinsearch As Boolean
    Dim _searchlabel As String
    Dim _width As Integer
    Dim _headerfmt As WebControls.HorizontalAlign
    Dim _cellfmt As WebControls.HorizontalAlign

 
    <Description("Détermine la largeur de la colonne"), Category("Colonne")> _
       Public Property ColWidth() As Integer
        Get

            If _width = 0 Then
                Return 40
            End If
            Return _width
        End Get
        Set(ByVal value As Integer)
            _width = value
        End Set
    End Property
    <Description("Détermine si la colonne est visible dans la grille du filtre"), Category("Grille")> _
    Public Property VisibleInFilter() As Boolean
        Get
            Return _visible
        End Get
        Set(ByVal value As Boolean)
            _visible = value
        End Set
    End Property
    <Description("Détermine la chaine de caractère qui met en forme la colonne dans la grille"), Category("Grille")> _
       Public Property Format() As String
        Get
            Return _format
        End Get
        Set(ByVal value As String)
            _format = value
        End Set
    End Property
    <Description("Alignement horizontal de la colonne"), Category("Grille")> _
          Public Property CellAlign() As WebControls.HorizontalAlign
        Get
            Return _cellfmt
        End Get
        Set(ByVal value As WebControls.HorizontalAlign)
            _cellfmt = value
        End Set
    End Property

    <Description("Le texte qui sera affiché pour l'entête de la colonne dans la grille"), Category("Grille")> _
       Public Property Header() As String
        Get
            Return _header
        End Get
        Set(ByVal value As String)
            _header = value
        End Set
    End Property
    <Description("Alignement horizontal de l'entête"), Category("Grille")> _
       Public Property HeaderAlign() As WebControls.HorizontalAlign
        Get
            Return _headerfmt
        End Get
        Set(ByVal value As WebControls.HorizontalAlign)
            _headerfmt = value
        End Set
    End Property
    <Description("Détermine si la colonne est visible dans la fenêtre de recherche"), Category("Recherche")> _
       Public Property ShowInSearch() As Boolean
        Get
            Return _showinsearch
        End Get
        Set(ByVal value As Boolean)
            _showinsearch = value
        End Set
    End Property
    <Description("Le texte qui sera affiché dans la fenêtre de recherche"), Category("Recherche")> _
      Public Property SearchLabel() As String
        Get
            Return _searchlabel
        End Get
        Set(ByVal value As String)
            _searchlabel = value
        End Set
    End Property

    <Category("Colonne")> Public Overrides Property ID() As String
        Get
            Return _ID
        End Get
        Set(ByVal Value As String)
            _ID = Value
        End Set
    End Property

    <Category("Colonne")> Public Property Key() As String
        Get
            Return _key
        End Get
        Set(ByVal Value As String)
            _key = Value
        End Set
    End Property


End Class

#End Region

#Region "Collection"

Public Class ColCollection
    'Définition des collections
    Inherits CollectionBase

    Default Public ReadOnly Property Item(ByVal pageIndex As Integer) As FltColumn
        Get
            Return MyBase.List(pageIndex)
        End Get
    End Property

    Public Sub Add(ByVal page As FltColumn)
        MyBase.List.Add(page)
    End Sub

    Public Function IndexOf(ByVal page As FltColumn) As Integer
        Return MyBase.List.IndexOf(page)
    End Function

    Public Function IndexOf(ByVal key As String) As Integer

        Dim myCol As FltColumn
        Dim i As Single = 0

        While (i < MyBase.List.Count)

            myCol = MyBase.List.Item(i)
            If (myCol.Key = key) Then

                Return i

            End If

            i = i + 1

        End While

        Return -1

    End Function
End Class

#End Region

#Region "Designer"

Public Class HQFilterDesigner
    Inherits System.Web.UI.Design.ControlDesigner

    Public Overrides Function getdesignTimeHTML() As String
        Dim writer As New System.IO.StringWriter
        Dim html As New HtmlTextWriter(writer)
        'Dim cmnDir As String
        Dim ctl As HQFilter = CType(Component, HQFilter)

        Dim strTxt As String = "Controle de filtre"
       
        writer.Write(("<DIV style=""LEFT: " & ctl.Style("LEFT") & "; " & _
                     "TOP: " & ctl.Style("TOP") & "; " & _
                     "POSITION: " & ctl.Style("POSITION") & "; " & _
                     "Z-INDEX: " & ctl.Style("Z-INDEX") & "; " & _
                     "WIDTH: " & ctl.FilterWidth + 10 & "; " & _
                     "HEIGHT: " & ctl.FilterHeight + 30 & """>") & vbCr)

        writer.Write(("<TABLE  id=""Table1"" cellSpacing=""1""") & vbCr)
        writer.Write(("                        cellPadding=""1"" height=""" & _
                      ctl.FilterHeight + 30 & """ width=""" & ctl.FilterWidth + 10 & _
                      """ border=""1"">") & vbCr)
        writer.Write(("                        <TR>") & vbCr)
        writer.Write(("                              <TD>" & strTxt & "</TD>") & vbCr)
        writer.Write(("                        </TR>") & vbCr)
        writer.Write(("                  </TABLE>") & vbCr)
        writer.Write(("</DIV>") & vbCr)


        Return writer.ToString
    End Function
End Class

#End Region



hqdevAsked:
Who is Participating?
 
hqdevConnect With a Mentor Author Commented:
I found another way around that problem, I put a new property in my column collection and I make it unique by validating that only one column has that property set.
0
 
traxionCommented:
I don't see FltColumn2 referenced anywhere in the code you provided
0
 
hqdevAuthor Commented:
FltColumn1..2.. ect  is generated when I had a columns to the collection with the property COLUMNS.
Those are childs object of the control.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
traxionCommented:
Ok...I would need to see the actual code you are using to accomplish this (class initializer, etc.) but here is what I see:

You are probably calling SortCol and passing in the string value of the ID of the FltColumn.  Does this make sense?

It would be like the following:
A Property of type textbox...
Public Property tb () As TextBox
      Get
           Return _tb
      End Get
      Set (ByVal value As TextBox)
               _tb = value
      End Set
End Property

Then I say:  myclass.tb = TextBox1.ID

So I'm trying to pass a string into the function and it would throw the error.  You need to obtain an actual reference to the FltColumn in question and pass that reference into the property instead of just the string based ID.
0
 
hqdevAuthor Commented:
All the code is in the message. If you go at the end you will see the FLTColumn class wich is the type of object I'm trying to use in the sortcol property.

But your example is right, that's similar to what I'm doing, but I don't even have to reference it in the code to get the error.
In the design page, I go to the property browser of my control and I select a column from the dropdown that shows in the SortCol property. (I have previously created 2 columns, so I see them in the dropdown).
Then I try to run the project and I'm getting the error.
0
 
traxionCommented:
Ok, I won't be able to look at this again for about 3 hours, but when I get home I'm going to throw the code into Visual Studio and troubleshoot.
0
All Courses

From novice to tech pro — start learning today.