代码之家  ›  专栏  ›  技术社区  ›  Charlie

在SharePoint中创建自定义EditorPart

  •  0
  • Charlie  · 技术社区  · 16 年前

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

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

    因此,当调用ApplyChanges函数时,所有控件都将返回默认设置。

    有人对此有什么建议吗?

    我根本无法理解这一点。然而,我看到了这一点,我也陷入了同样的困境,当CreateChildCOntrols()总是首先运行时,如何将任何内容保存回ApplyChanges()中的web部件,从而用一个新实例替换我的复选框列表,因此没有选择的项。我在下面包含了完整的代码,希望我是一个十足的傻瓜,解决方案显而易见。

    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 回复  |  直到 16 年前
        1
  •  0
  •   ArjanP    16 年前

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

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

        2
  •  0
  •   Tudor Olariu    16 年前

    这个 ()方法的作用正好相反,它获取Web部件中以前存储的属性,并相应地更新编辑器。在创建自定义编辑器时,您有责任为它们编写逻辑。

        3
  •  0
  •   Charlie    16 年前

    实际代码应该如下所示:

    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
    

    推荐文章