Search code examples
sharepointmossweb-parts

Creating a custom EditorPart in SharePoint


I have used the following article as a guide to creating a custom EditorPart in SharePoint

http://blah.winsmarts.com/2006/05/19/writing-custom-editors-for-sharepoint-2007-and-aspnet-20-webparts.aspx

However when I implement this technique I cannot save the changes to my custom properties. Basically the CreateChildControls function is called when the 'Apply' or 'Save' buttons are used and this creates a new instance of my internal control variable, thus wiping any changes the user has made.

So when the ApplyChanges function is called all the controls are back to the default settings.

Does anyone have any advice on this?

Thanks

UPDATE

I am just not able to wrap my head around this at all. However I look at this I get stuck at the same point, how can I save anything back to my web part in ApplyChanges() when CreateChildCOntrols() is always run first thus replacing my CheckBoxList with a new instance and therefore no selected items. I have included the full code below in the hope that I am being a total dunce and the solution is obvious.

Private Class CaseMatterInfoEditorPart
    Inherits EditorPart

    Protected WithEvents _propList As CheckBoxList

    Protected Overrides Sub CreateChildControls()

        _propList = New CheckBoxList
        _propList.EnableViewState = True
        _propList.AutoPostBack = True
        _propList.Width = New Unit("100%")

        LoadProperties()

        Me.Controls.Add(New LiteralControl("Please select the data items you wish to include:<br />"))
        Me.Controls.Add(_propList)

    End Sub


    Public Overrides Function ApplyChanges() As Boolean

        Dim part As CaseMatterInfoPart = CType(WebPartToEdit,  _
                                               CaseMatterInfoPart)

        If part IsNot Nothing Then
            GetSelectedDataValues()
        Else
            Return False
        End If

        Return True

    End Function

    Public Overrides Sub SyncChanges()

        EnsureChildControls()

        Dim part As CaseMatterInfoPart = CType(WebPartToEdit,  _
                                               CaseMatterInfoPart)

        If part IsNot Nothing Then

            If Not String.IsNullOrEmpty(part.DataValues) Then
                SetSelectedValues(part.DataValues)
            End If

        End If


    End Sub

    Private Function GetSelectedDataValues() As String

        Dim strReturn As String = ""

        For Each item As ListItem In _propList.Items

            If item.Selected Then
                strReturn &= item.Text & "|"
            End If

        Next

        If Not String.IsNullOrEmpty(strReturn) Then
            strReturn = strReturn.Remove(strReturn.Length - 1, 1)
        End If

        Return strReturn

    End Function

    Private Sub SetSelectedValues(ByVal Values As String)

        If Not String.IsNullOrEmpty(Values) And _
            _propList IsNot Nothing Then

            _propList.ClearSelection()

            Dim split() As String = Values.Split("|")

            For Each strValue As String In split

                For Each item As ListItem In _propList.Items

                    If item.Text = strValue Then
                        item.Selected = True
                    End If

                Next

            Next

        End If

    End Sub

    Private Sub LoadProperties()

        Dim file As New File
        Dim lstProperties As List(Of String) = GetStringPropertyNames(file.GetType)

        For Each strProperty As String In lstProperties

            _propList.Items.Add(strProperty)

        Next

    End Sub

    Private Function GetStringPropertyNames(ByVal Type As System.Type) As List(Of String)

        Dim props() As PropertyInfo = Type.GetProperties
        Dim propList As New List(Of String)

        For Each prop As PropertyInfo In props

            If prop.Name <> "Chronology" And _
                    prop.Name <> "Documents" And _
                    prop.Name <> "Milestones" And _
                    prop.Name <> "DiaryEntries" And _
                    prop.Name <> "FileLoadSuccesful" And _
                    prop.Name <> "FileLoadError" Then

                Dim boo As Boolean = False
                Dim bootype As Type = boo.GetType
                Dim dec As Decimal
                Dim decType As Type = dec.GetType

                If prop.PropertyType Is "".GetType Or _
                    prop.PropertyType Is Now.GetType Or _
                    prop.PropertyType Is bootype Or _
                    prop.PropertyType Is decType Then

                    propList.Add(prop.Name)

                Else

                    Dim listChildPropertyStrings As List(Of String) = GetStringPropertyNames(prop.PropertyType)

                    For Each strProp As String In listChildPropertyStrings

                        propList.Add(prop.Name & ": " & strProp)

                    Next

                End If

            End If


        Next

        Return propList

    End Function

End Class

Hope someone out there can see what I cannot.

Thanks


Solution

  • It would help if I had actually saved the string returned from GetSelectedDataValues() into a property on the web part...

    Actual code should look like this:

    Public Overrides Function ApplyChanges() As Boolean        
        Dim part As CaseMatterInfoPart = CType(WebPartToEdit, CaseMatterInfoPart)        
        If part IsNot Nothing Then            
            part.DataValues = GetSelectedDataValues()        
        Else            
            Return False        
        End If        
        Return True    
    End Function
    

    It so often pays to check, double check and triple check what you are doing. I had serioulsy over complicated this issue looking for an answer that was staring me in the face.