【问题标题】:Sharepoint 2010 Custom propertiesSharepoint 2010 自定义属性
【发布时间】:2012-02-06 11:46:55
【问题描述】:
我正在为 Sharepoint 2010 构建 Web 部件。我可以创建可通过 Sharepoint 用户界面编辑的自定义属性。没问题。
问题是:我想使用自定义对象 (Properties.cs) 来定义相同的属性(并保持编辑功能可用),而不是像 Internet 上显示的那样将所有代码转储到 Webpart.cs 中。
有没有办法做到这一点?因为我不想在 webpart 类中抽取我所有的属性(可编辑或不可编辑)。
【问题讨论】:
标签:
sharepoint-2010
custom-properties
【解决方案1】:
是的,你可以做到...通过使用继承和创建基类,如下所示
1- 首先创建一个继承自 WebPart 类的基类,并覆盖 CreateChildControls 方法,例如
<XmlRoot("MyWebPartBase")> _
<ToolboxItemAttribute(True)> _
Public Class BaseWebPart
Inherits WebPart
Protected Overrides Sub CreateChildControls()
Dim control As Object = Page.LoadControl(ascxPath)
If control IsNot Nothing Then
control.WebPartControl = Me
Controls.Add(CType(control, Control))
End If
End Sub
'Add public properties here
End Class
2- 在这个基类中实现您的属性,并从上述基类而不是 webpart 类继承您的 webpart。
3- 为实现公共属性的用户控件创建一个基类,以便在用户控件中访问它们,例如
Public Class BaseUserControl
Inherits UserControl
Private _WebPartControl As BaseWebPart
Public Property WebPartControl As BaseWebPart
Get
Return _WebPartControl
End Get
Set(ByVal value As BaseWebPart)
_WebPartControl = value
End Set
End Property
Public ReadOnly Property WebPartID() As String
Get
Return WebPartControl.ID
End Get
End Property
End Class