2
votes

I want to add a List(Of Book) property to my user control. I defined a Book class and BookCollectionEditor class for CollectionEditor. And also I defined a public property named BookList for my user control. For a custom control it works fine but for user control, the designer does not display my property. On markup I can add a Book item but on designer it gives error: "Type 'System.Web.UI.UserControl' does not have a public property named 'BookList'."

Isn't is possible to define a list property for a user control?

<TypeConverter(GetType(ExpandableObjectConverter)), Serializable()> _
Public Class Book
  Private _name As String
  Private _author As String

  Public Sub New()
    Me.New(String.Empty, String.Empty)
  End Sub

  Public Sub New(ByVal name As String, ByVal author As String)
    _name = name
    _author = author
  End Sub

  <Category("Book Property"), Description("Name of the Book"), DefaultValue(""), NotifyParentProperty(True)> _
  Public Property Name() As String
    Get
      Return _name
    End Get
    Set(ByVal value As String)
      _name = value
    End Set
  End Property

  <Category("Book Property"), Description("Author of the Book"), DefaultValue(""), NotifyParentProperty(True)> _
  Public Property Author() As String
    Get
      Return _author
    End Get
    Set(ByVal value As String)
      _author = value
    End Set
  End Property
End Class

Public Class BookCollectionEditor
  Inherits CollectionEditor

  Public Sub New(ByVal newType As Type)
    MyBase.new(newType)
  End Sub

  Protected Overrides Function CanSelectMultipleInstances() As Boolean
    Return False
  End Function

  Protected Overrides Function CreateCollectionItemType() As Type
    Return GetType(Book)
  End Function
End Class

Partial Class BooksUserControl
  Inherits System.Web.UI.UserControl

  Private _booklist As New List(Of Book)

  <Category("Behavior"), _
   Description("Book List"), _
   DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
   NotifyParentProperty(True), _
   PersistenceMode(PersistenceMode.InnerProperty), _
   Editor(GetType(BookCollectionEditor), GetType(BookCollectionEditor)), _
   Browsable(True)> _
  Public ReadOnly Property BookList() As List(Of Book)
    Get
      If _booklist Is Nothing Then
        _booklist = New List(Of Book)()
      End If
      Return _booklist
    End Get
  End Property
End Class

Default.aspx

<uc1:BooksUserControl ID="BooksUserControl1" runat="server">
  <BookList>
  </BookList>
</uc1:BooksUserControl>
1

1 Answers

0
votes

Yes it is possible to define List(Of T) property. Try your code without Editor setting to see if the problem lies in that part of code.