Creazione di una Editor Editor personalizzata in SharePoint
-
06-07-2019 - |
Domanda
Ho usato il seguente articolo come guida per la creazione di una EditorPart personalizzata in SharePoint
Tuttavia, quando implemento questa tecnica, non riesco a salvare le modifiche alle mie proprietà personalizzate. Fondamentalmente la funzione CreateChildControls viene chiamata quando vengono utilizzati i pulsanti 'Applica' o 'Salva' e questo crea una nuova istanza della mia variabile di controllo interno, cancellando così tutte le modifiche apportate dall'utente.
Quindi, quando viene chiamata la funzione ApplyChanges, tutti i controlli tornano alle impostazioni predefinite.
Qualcuno ha qualche consiglio al riguardo?
Grazie
UPDATE
Non riesco proprio a capirci nulla. Tuttavia, guardo questo, rimango bloccato nello stesso punto, come posso salvare qualsiasi cosa nella mia web part in ApplyChanges () quando CreateChildCOntrols () viene sempre eseguito per primo sostituendo così la mia CheckBoxList con una nuova istanza e quindi senza elementi selezionati. Ho incluso il codice completo di seguito nella speranza di essere un somaro totale e la soluzione è ovvia.
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
Spero che qualcuno là fuori possa vedere cosa non posso.
Grazie
Soluzione 3
Sarebbe utile se avessi effettivamente salvato la stringa restituita da GetSelectedDataValues ??() in una proprietà sulla web part ...
Il codice effettivo dovrebbe essere simile al seguente:
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
Molto spesso vale la pena controllare, ricontrollare e triplicare ciò che si sta facendo. Ho avuto seriamente complicato questo problema alla ricerca di una risposta che mi stesse fissando in faccia.
Altri suggerimenti
Di solito avvio EditorPart con il codice in questo articolo: http://msdn.microsoft. com / it-it / library / system.web.ui.webcontrols.webparts.editorpart.aspx non ho mai avuto problemi del genere.
Sembra che tu abbia un problema con il flusso di controllo in ASP.NET ma senza codice è difficile vedere di cosa si tratta.
Il metodo ApplyChanges () viene utilizzato per acquisire i contenuti dell'editor personalizzato e applicarli alla webpart, mentre il metodo SyncChanges () fa il contrario, esso prende le proprietà precedentemente memorizzate nella webpart e aggiorna di conseguenza l'editor. È tua responsabilità scrivere la logica per entrambi durante la creazione di un editor personalizzato.