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.

Rickjaah
  • 581
  • 1
  • 6
  • 16

1 Answers1

0

Yes, you can do it... by using inheritance and creating base classes as follows

1- first create a base class inheriting from WebPart class with override CreateChildControls method e.g

<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- Implement you properties in this base class and Inherent your webparts from above mentioned base class rather then webpart class.

3- Create a base class for user controls implementing public properties to access them in user control e.g.

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
Salman Aziz
  • 351
  • 1
  • 2
  • 10