Page view counter

Custom Control With Multiple Properties of Collection Type

Last post 07-20-2004 7:01 PM by superconsultant. 5 replies.

Sort Posts:

  • Custom Control With Multiple Properties of Collection Type

    07-19-2004, 6:26 PM
    Does any one know if it is posible to have more than one propery of collection type on custom server control.

    It seems that it is requred to put property name in to ParseChildere tag of the control, something like:
    ParseChildren(True, "MyProperty1") in order for the property items to be recognized. But where would I put second property's name?

    If you know the answer please advice.
  • Re: Custom Control With Multiple Properties of Collection Type

    07-20-2004, 2:30 AM
    • Loading...
    • Andy Smith
    • Joined on 06-10-2002, 8:47 PM
    • Denver, CO
    • Posts 380
    • Points 1,910
    • ASPInsiders
    that second param is the "default property". If you only have one collection, you probably want to use it. However, if you have two properties, leave it as ParseChildren( True ).
    Then your server tag will look like this:
    <foo:MyControl ... >
    
    <MyCollectionProperty1>
    <foo:Item1 />
    <foo:Item2 />
    </MyCollectionProperty1>
    <MyCollectionProperty2>
    <foo:Item1 />
    <foo:Item2 />
    </MyCollectionProperty2>
    </foo:MyControl>
  • Re: Custom Control With Multiple Properties of Collection Type

    07-20-2004, 11:22 AM
    When I try just ParseChildren(true) what I get using your example:

    <foo:MyControl ... >

    <foo:Item1 />

    <foo:Item2 />

    <foo:Item1 />

    <foo:Item2 />

    </foo:MyControl>

    where <MyCollectionProperty1> and <MyCollectionProperty2> tags are missing. So when I run it it gives me an error someting like <foo:MyControl> does not have property <foo:Item1/>.

    Maybe if you have time you can take a look at my code bellow and see what's wrong or what tags I am missing:


    ---------CONTROL CLASS------------
    Imports System.Web.UI
    Imports System.ComponentModel
    <ParseChildren(True), PersistChildren(False), _
    PersistenceMode(PersistenceMode.InnerProperty), _
    ToolboxData("<{0}:EmployeeControl runat=server></{0}:EmployeeControl>")> _
    Public Class EmployeeControl
    Inherits System.Web.UI.WebControls.WebControl

    Private _empCollection As New EmployeeCollection
    Private _userCollection As New UserCollection

    <Category("Custom"), NotifyParentProperty(True), _
    PersistenceMode(PersistenceMode.InnerDefaultProperty), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property UserInfos() As UserCollection
    Get

    Return _userCollection
    End Get
    Set(ByVal Value As UserCollection)
    _userCollection = Value
    End Set
    End Property

    <Category("Custom"), NotifyParentProperty(True), _
    PersistenceMode(PersistenceMode.InnerDefaultProperty), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property EmployeeInfos() As EmployeeCollection
    Get

    Return _empCollection
    End Get
    Set(ByVal Value As EmployeeCollection)
    _empCollection = Value
    End Set
    End Property

    End Class

    -----------COLLECTION CLASSES-------------

    Imports System.ComponentModel
    Imports System.Web.UI

    Public Class UserCollection
    Inherits CollectionBase

    Default Public Property Item(ByVal index As Integer) As UserInfo
    Get
    Return CType(List(index), UserInfo)
    End Get
    Set(ByVal Value As UserInfo)
    List(index) = Value
    End Set
    End Property

    Public Function Add(ByVal value As UserInfo) As Integer
    Return List.Add(value)
    End Function 'Add

    Public Function IndexOf(ByVal value As UserInfo) As Integer
    Return List.IndexOf(value)
    End Function 'IndexOf

    Public Sub Insert(ByVal index As Integer, ByVal value As UserInfo)
    List.Insert(index, value)
    End Sub 'Insert

    Public Sub Remove(ByVal value As UserInfo)
    List.Remove(value)
    End Sub 'Remove
    End Class

    -----------------------

    Imports System.ComponentModel
    Imports System.Web.UI

    Public Class EmployeeCollection
    Inherits CollectionBase

    Default Public Property Item(ByVal index As Integer) As EmployeeInfo
    Get
    Return CType(List(index), EmployeeInfo)
    End Get
    Set(ByVal Value As EmployeeInfo)
    List(index) = Value
    End Set
    End Property

    Public Function Add(ByVal value As EmployeeInfo) As Integer
    Return List.Add(value)
    End Function 'Add

    Public Function IndexOf(ByVal value As EmployeeInfo) As Integer
    Return List.IndexOf(value)
    End Function 'IndexOf

    Public Sub Insert(ByVal index As Integer, ByVal value As EmployeeInfo)
    List.Insert(index, value)
    End Sub 'Insert

    Public Sub Remove(ByVal value As EmployeeInfo)
    List.Remove(value)
    End Sub 'Remove
    End Class

    ------------TYPE CLASSES----------------

    Imports System.ComponentModel

    Public Class UserInfo
    Public strFirstName As String = ""
    Public strLastName As String = ""
    Public iUserId As Integer = 0
    Property [FirstName]() As String
    Get
    Return strFirstName
    End Get
    Set(ByVal Value As String)
    strFirstName = Value
    End Set
    End Property

    Property [LastName]() As String
    Get
    Return strLastName
    End Get
    Set(ByVal Value As String)
    strLastName = Value
    End Set
    End Property

    Property [UserID]() As Integer 'UserIDCollection
    Get
    Return iUserId
    End Get
    Set(ByVal Value As Integer) 'UserIDCollection)
    iUserId = Value
    End Set
    End Property
    End Class

    ----------------

    Imports System.ComponentModel

    Public Class EmployeeInfo
    Private strFirstName As String = ""
    Private strLastName As String = ""
    Private iEmpId As Integer = 0

    Property [FirstName]() As String
    Get
    Return strFirstName
    End Get
    Set(ByVal Value As String)
    strFirstName = Value
    End Set
    End Property

    Property [LastName]() As String
    Get
    Return strLastName
    End Get
    Set(ByVal Value As String)
    strLastName = Value
    End Set
    End Property

    Property [EmployeeId]() As Integer
    Get
    Return iEmpId
    End Get
    Set(ByVal Value As Integer)
    iEmpId = Value
    End Set
    End Property
    End Class

    ---------------------------
    Your help is greately appriciated.

    Thanks and Regards





  • Re: Custom Control With Multiple Properties of Collection Type

    07-20-2004, 12:20 PM
    Andy,

    It seems I found the problem:

    PersistenceMode(PersistenceMode.InnerDefaultProperty) that I have there should be changed to PersistenceMode(PersistenceMode.InnerProperty). That made it to look correctly, exactly the way you have it in your example.

    However I still one strange problem left. When I run a page with my control in it it runs fine, but when I exit Visual Studio.NET and get back my control on a page displayed with the error message: Error Creating Control and tooptip saying: '' could not be set on property 'UserInfos' and it still runs fine, but I can not edit its properties in property panel.

    If you have any ideas please let me know.

    Regards,
    Dimitry
  • Re: Custom Control With Multiple Properties of Collection Type

    07-20-2004, 2:26 PM
    • Loading...
    • danfizesan
    • Joined on 04-05-2003, 11:19 AM
    • Posts 8
    • Points 40
    Delete the "Set" accesor to the UserInfos property and you'll be fine.

    Leave it as follows :

    Public Property UserInfos() As UserCollection
    Get
    Return _userCollection
    End Get
    End Property

    Best regards,
    Dan
  • Re: Custom Control With Multiple Properties of Collection Type

    07-20-2004, 6:00 PM
    It worked Dan!

    Thank you very much.

    Regards,
    Dimitry
Page 1 of 1 (6 items)