对于一个复合控件,其属性一般都是委托到子控件的属性。
但有时,我们需要将子控件的很多属性暴露出来。如果没有很好的分类管理,在属性浏览器里会很混乱。我在实际的开发过程中,是将子控件作为一个属性保存的。实现方法如下:
首先我的复合控件有一个datagrid,两个button,一个textbox。
复合控件(CompositeControl)继承于webcontrol,同时为其创建了builder类和designer类。
Builder类:
Public Overrides Function GetChildControlType(ByVal tagName As String, ByVal attribs As ystem.Collections.IDictionary) As System.Type
If tagName.ToLower().EndsWith("textbox") Then
Return GetType(TextBox)
End If
If tagName.ToLower().EndsWith("datagrid") Then
Return GetType(DataGrid)
End If
If tagName.ToLower().EndsWith("button") Then
Return GetType(Button)
End If
Return Nothing
End Function
Designer类:
Public Overrides Function GetPersistInnerHtml() As String
Dim strWriter As StringWriter
Dim htmlWriter As HtmlTextWriter
Dim control As PressListBox
strWriter = New StringWriter
htmlWriter = New HtmlTextWriter(strWriter)
control = CType(Me.Component, CompositeControl)
MyBase.GetPersistInnerHtml()
htmlWriter.Write(ControlPersister.PersistControl(control.TextBox))
htmlWriter.Write(ControlPersister.PersistControl(control.DataGrid))
htmlWriter.Write(ControlPersister.PersistControl(control.NextButton))
htmlWriter.Write(ControlPersister.PersistControl(control.PreviousButton))
Return strWriter.ToString
End Function
复合控件属性:
<PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True), _
Description("Page Count"), Category("ChildControl")> _
ReadOnly Property pageTextBox() As TextBox
Get
Me.EnsureChildControls()
Return txtPageCount
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True), _
Description("DisplayData"), Category("ChildControl")> _
ReadOnly Property PageDataGrid() As NotifyDataGrid
Get
Me.EnsureChildControls()
Return dgdList
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), _
Description("「<」"), _
NotifyParentProperty(True)> ReadOnly Property PreviousPageButton() As Button
Get
Me.EnsureChildControls()
Return btnPrevious
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), _
Description("「>」"), _
NotifyParentProperty(True)> ReadOnly Property NextPageButton() As Button
Get
Me.EnsureChildControls()
Return btnNext
End Get
End Property
同时重载AddParsedSubObject方法:
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
Dim idName As Array
If TypeOf (obj) Is TextBox Then
Me.txtPageCount = obj
End If
If TypeOf (obj) Is DataGrid Then
Me.dgdList = obj
End If
If TypeOf (obj) Is Button Then
idName = Split(CType(obj, Button).ID.ToString, "_")
If idName(1).ToString.Equals("NextPageButton") Then
Me.btnNext = obj
Else
Me.btnPrevious = obj
End If
End If
End Sub
但有时,我们需要将子控件的很多属性暴露出来。如果没有很好的分类管理,在属性浏览器里会很混乱。我在实际的开发过程中,是将子控件作为一个属性保存的。实现方法如下:
首先我的复合控件有一个datagrid,两个button,一个textbox。
复合控件(CompositeControl)继承于webcontrol,同时为其创建了builder类和designer类。
Builder类:
Public Overrides Function GetChildControlType(ByVal tagName As String, ByVal attribs As ystem.Collections.IDictionary) As System.Type
If tagName.ToLower().EndsWith("textbox") Then
Return GetType(TextBox)
End If
If tagName.ToLower().EndsWith("datagrid") Then
Return GetType(DataGrid)
End If
If tagName.ToLower().EndsWith("button") Then
Return GetType(Button)
End If
Return Nothing
End Function
Designer类:
Public Overrides Function GetPersistInnerHtml() As String
Dim strWriter As StringWriter
Dim htmlWriter As HtmlTextWriter
Dim control As PressListBox
strWriter = New StringWriter
htmlWriter = New HtmlTextWriter(strWriter)
control = CType(Me.Component, CompositeControl)
MyBase.GetPersistInnerHtml()
htmlWriter.Write(ControlPersister.PersistControl(control.TextBox))
htmlWriter.Write(ControlPersister.PersistControl(control.DataGrid))
htmlWriter.Write(ControlPersister.PersistControl(control.NextButton))
htmlWriter.Write(ControlPersister.PersistControl(control.PreviousButton))
Return strWriter.ToString
End Function
复合控件属性:
<PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True), _
Description("Page Count"), Category("ChildControl")> _
ReadOnly Property pageTextBox() As TextBox
Get
Me.EnsureChildControls()
Return txtPageCount
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(True), _
Description("DisplayData"), Category("ChildControl")> _
ReadOnly Property PageDataGrid() As NotifyDataGrid
Get
Me.EnsureChildControls()
Return dgdList
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), _
Description("「<」"), _
NotifyParentProperty(True)> ReadOnly Property PreviousPageButton() As Button
Get
Me.EnsureChildControls()
Return btnPrevious
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty), Category("ChildControl"), _
Description("「>」"), _
NotifyParentProperty(True)> ReadOnly Property NextPageButton() As Button
Get
Me.EnsureChildControls()
Return btnNext
End Get
End Property
同时重载AddParsedSubObject方法:
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
Dim idName As Array
If TypeOf (obj) Is TextBox Then
Me.txtPageCount = obj
End If
If TypeOf (obj) Is DataGrid Then
Me.dgdList = obj
End If
If TypeOf (obj) Is Button Then
idName = Split(CType(obj, Button).ID.ToString, "_")
If idName(1).ToString.Equals("NextPageButton") Then
Me.btnNext = obj
Else
Me.btnPrevious = obj
End If
End If
End Sub
博客介绍复合控件属性管理问题,当需暴露子控件众多属性时,若无分类管理会使属性浏览器混乱。作者在开发中把复合控件的子控件作为属性保存,给出了复合控件继承及Builder类、Designer类实现代码,还展示了复合控件属性及重载AddParsedSubObject方法的代码。
2272

被折叠的 条评论
为什么被折叠?



