【问题标题】:Creating a custom EditorPart in SharePoint在 SharePoint 中创建自定义 EditorPart
【发布时间】:2010-11-04 07:01:16
【问题描述】:

我使用以下文章作为在 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

希望有人能看到我看不到的东西。

谢谢

【问题讨论】:

    标签: sharepoint moss web-parts


    【解决方案1】:

    我通常以本文中的代码开始我的 EditorPart: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.editorpart.aspx 从来没有遇到过这样的问题。

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

    【讨论】:

    • 谢谢,我已更新问题以包含代码。这对我来说真是个令人头疼的问题,我只知道这将是显而易见的。
    【解决方案2】:

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

    【讨论】:

    • 谢谢你,我知道函数应该做什么。我在上面发布的代码中看到的观点是,我无法在 ApplyChanges() 中保存数据,因为 CreateChildControls() 将运行并创建我的 CheckBoxList 的新实例。
    【解决方案3】:

    如果我真的将 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
    

    经常检查、仔细检查和三次检查你在做什么是值得的。我对这个问题的复杂化非常认真,正在寻找一个让我眼前一亮的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-18
      • 2011-08-26
      • 2014-11-17
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      相关资源
      最近更新 更多