2
我想添加List(Of Book)屬性到我的用戶控件。我爲CollectionEditor定義了Book類和BookCollectionEditor類。而且我爲我的用戶控件定義了一個名爲BookList的公共屬性。對於自定義控件,它可以正常工作,但對於用戶控件,設計者不顯示我的屬性。在標記上,我可以添加一個Book項目,但是在設計器上會給出錯誤:「Type'System.Web.UI.UserControl'沒有名爲'BookList'的公共屬性。用戶控制列表屬性設計器錯誤
不可能爲用戶控件定義列表屬性嗎?
<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>
但是,如何在沒有編輯器設置的情況下從屬性窗口添加項目? – egurer