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
希望有人能看到我看不到的东西。