Crear un EditorPart personalizado en SharePoint
-
06-07-2019 - |
Pregunta
He usado el siguiente artículo como guía para crear un EditorPart personalizado en SharePoint
Sin embargo, cuando implemento esta técnica, no puedo guardar los cambios en mis propiedades personalizadas. Básicamente, se llama a la función CreateChildControls cuando se usan los botones 'Aplicar' o 'Guardar' y esto crea una nueva instancia de mi variable de control interno, borrando así cualquier cambio que el usuario haya realizado.
Entonces, cuando se llama a la función ApplyChanges, todos los controles vuelven a la configuración predeterminada.
¿Alguien tiene algún consejo sobre esto?
Gracias
ACTUALIZACIÓN
Simplemente no puedo entender esto en absoluto. Sin embargo, veo que me atasco en el mismo punto, ¿cómo puedo guardar algo en mi elemento web en ApplyChanges () cuando CreateChildCOntrols () siempre se ejecuta primero, por lo que reemplaza mi CheckBoxList con una nueva instancia y, por lo tanto, no hay elementos seleccionados. He incluido el código completo a continuación con la esperanza de que soy un tonto total y la solución es obvia.
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
Espero que alguien pueda ver lo que yo no puedo.
Gracias
Solución 3
Sería útil si realmente hubiera guardado la cadena devuelta desde GetSelectedDataValues ??() en una propiedad en el elemento web ...
El código real debería verse así:
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
A menudo vale la pena verificar, verificar dos veces y verificar tres veces lo que está haciendo. Tuve una seriedad sobre este problema complicado en busca de una respuesta que me estaba mirando a la cara.
Otros consejos
Normalmente comienzo mi EditorPart con el código de este artículo: http://msdn.microsoft. com / es-us / library / system.web.ui.webcontrols.webparts.editorpart.aspx nunca tuve tales problemas.
Parece que tiene un problema con el flujo de control en ASP.NET pero sin código es difícil ver qué es.
El método ApplyChanges () se usa para tomar el contenido del editor personalizado y aplicarlo al elemento web, mientras que el método SyncChanges () hace lo contrario, toma las propiedades almacenadas previamente en el elemento web y actualiza el editor en consecuencia. Es su responsabilidad escribir la lógica de ambos al crear un editor personalizado.