views:

1039

answers:

3

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

A: 

I usually start my EditorPart with the code in this article: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.editorpart.aspx never had any such problems.

Sounds like you have a problem with the control flow in ASP.NET but without code it is hard to see what it is.

ArjanP
Thank you, I have updated the question to include the code. This is a real head wrecker for me and I just know it is going to be something obvious.
Charlie
A: 

The ApplyChanges() method is used to take the contents of the custom editor and apply them to the webpart, while the SyncChanges() method does the opposite, it takes the previously stored properties in the webpart and updates the editor accordingly. It is your responsibility to write the logic for both of them when creating a custom editor.

Tudor Olariu
Thank you for that, I know what the functions are supposed to do. My point as seen in the code posted above is that I cannot save data in ApplyChanges() as CreateChildControls() will have run and created a new instance of my CheckBoxList.
Charlie
A: 

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.

Charlie