0

I'm building a webpart for Sharepoint 2010. I can create custom properties which are editable through the Sharepoint user interface. No problem there.

The problem is: I want to use a custom object(Properties.cs) to define those same properties(and keeping the editing functionality available), rather than dumping all code in the Webpart.cs like it's shown on the internet.

Is there a way to do this? Because I don't want to pump all my properties(editable or not) in the webpart class.

4

1 回答 1

0

是的,你可以做到...通过使用继承和创建基类如下

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
于 2012-04-07T18:02:46.723 回答