在SharePoint中创建自定义EditorPart

时间:2009-06-19 12:02:53

标签: sharepoint moss web-parts

我使用以下文章作为在SharePoint中创建自定义EditorPart的指南

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

但是,当我实现此技术时,我无法将更改保存到自定义属性中。基本上,当使用“应用”或“保存”按钮时,会调用CreateChildControls函数,这会创建内部控制变量的新实例,从而擦除用户所做的任何更改。

因此,当调用ApplyChanges函数时,所有控件都恢复为默认设置。

有人对此有任何建议吗?

由于

更新

我根本无法解决这个问题。但是我看到这个问题我被困在同一点上,当CreateChildCOntrols()总是首先运行时,如何在ApplyChanges()中将任何内容保存回我的Web部件,从而用新实例替换我的CheckBoxList,因此没有选定的项目。我已经将下面的完整代码包括在内,希望我能够完全投入使用并且解决方案很明显。

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

希望有人可以看到我不能做的事。

由于

3 个答案:

答案 0 :(得分:0)

我通常使用本文中的代码启动我的EditorPart: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.editorpart.aspx 从未遇到任何此类问题。

听起来你在ASP.NET中控制流有问题,但没有代码就很难看出它是什么。

答案 1 :(得分:0)

ApplyChanges ()方法用于获取自定义编辑器的内容并将其应用于webpart,而 SyncChanges ()方法则相反,获取webpart中先前存储的属性并相应地更新编辑器。在创建自定义编辑器时,您有责任为它们编写逻辑。

答案 2 :(得分:0)

如果我实际上将GetSelectedDataValues()返回的字符串保存到Web部件的属性中会有所帮助...

实际代码应如下所示:

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

通常需要检查,仔细检查并三重检查您正在做什么。我在复杂的问题上遇到了严重的问题,正在寻找一个盯着我的答案。