【发布时间】:2010-11-04 07:01:16
【问题描述】:
我使用以下文章作为在 SharePoint 中创建自定义 EditorPart 的指南
但是,当我实施此技术时,我无法将更改保存到我的自定义属性中。基本上,当使用“应用”或“保存”按钮时会调用 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