2016-02-28 4 views
0

Я попытался разработать веб-часть, которая отображает текст, введенный в Свойстве настраиваемого EditorPart, , но у меня проблема с сохранением свойств. Когда я нажимаю «ОК» или «Сохранить» и открываю свойства WebPart, значения свойств возвращаются к умолчанию, То же самое, когда я сохраняю страницу после редактирования свойств WebPart и нажатия на кнопки Apply OR OK. Ниже приведен код, я реализован:Свойства WebPart Стойкость

using System; 
using System.ComponentModel; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using Microsoft.SharePoint.WebPartPages; 
using System.Web.UI.WebControls.WebParts; 
using System.Collections.Generic; 
using Microsoft.SharePoint.Administration; 

namespace SH_Perso.WebPartPerso 
{ 
    [ToolboxItemAttribute(false)] 
    public class WebPartPerso : System.Web.UI.WebControls.WebParts.WebPart 
    { 

     [WebBrowsable(false), Personalizable(PersonalizationScope.User), DefaultValue("PERSO")] 
     public string MyPortalChoice { get; set; } 
     [WebBrowsable(false), Personalizable(PersonalizationScope.User), DefaultValue("Documents")] 
     public string MyAppChoice { get; set; } 
     [WebBrowsable(false), Personalizable(PersonalizationScope.User), DefaultValue("Tous les éléments")] 
     public string MyViewChoice { get; set; } 




     protected override void CreateChildControls() 
     { 
      base.CreateChildControls(); 



      Controls.Add(new LiteralControl("PORTAIL :" + MyPortalChoice + "<br/> APPLICATION : " + MyAppChoice + "<br/> VUE : " + MyViewChoice)); 

     } 




     /// <summary> 
     /// Creates custom editor parts here and assigns a unique id to each part 
     /// </summary> 
     /// <returns>All custom editor parts used by this web part</returns> 
     public override EditorPartCollection CreateEditorParts() 
     { 

      PEditorPart editorPart = new PEditorPart(); 
      editorPart.Title = "Choix de la liste à afficher"; 
      editorPart.ID = ID + "_editorPart"; 

      EditorPartCollection editors = new EditorPartCollection(base.CreateEditorParts(), new EditorPart[] { editorPart }); 

      return editors; 
     } 


     public override object WebBrowsableObject 
     { 
      get 
      { 
       return(this); 
      } 
     } 




    } 
} 

// THE EDITOR PART CLASS 
    class PEditorPart : EditorPart 
    { 


     public TextBox PortalChoices; 
     public TextBox AppChoices; 
     public TextBox ListViews; 

     protected override void OnInit(EventArgs e) 
     { 
      base.OnInit(e); 

      PortalChoices = new TextBox(); 
      AppChoices = new TextBox(); 
      ListViews = new TextBox(); 
     } 

     protected override void CreateChildControls() 
     { 




       Controls.Add(new LiteralControl("<div> <span style='font-weight:bold;'>Portail</span> ")); 
       Controls.Add(PortalChoices); 
       Controls.Add(new LiteralControl("</div>")); 
       Controls.Add(new LiteralControl("<div> <span style='font-weight:bold;'>Listes disponibles</span> ")); 
       Controls.Add(AppChoices); 
       Controls.Add(new LiteralControl("</div>")); 
       Controls.Add(new LiteralControl("<div> <span style='font-weight:bold;'>Listes des vues disponibles</span> ")); 
       Controls.Add(ListViews); 
       Controls.Add(new LiteralControl("</div>")); 


       base.CreateChildControls(); 
       ChildControlsCreated = true; 

     } 



     /// <summary> 
     /// Applies change in editor part ddl to the parent web part 
     /// </summary> 
     /// <returns></returns> 
     public override bool ApplyChanges() 
     { 
      try 
      { 

      EnsureChildControls(); 
      WebPartPerso ParentWebPart = (WebPartPerso)WebPartToEdit; 

      if (ParentWebPart != null) 
      { 
       ParentWebPart.MyAppChoice = AppChoices.Text; 
       ParentWebPart.MyViewChoice = ListViews.Text; 
       ParentWebPart.MyPortalChoice = PortalChoices.Text; 

      } 
      ParentWebPart.SaveChanges(); 
      // The operation was succesful 
      return true; 
      } 
      catch 
      { 
       // Because an error has occurred, the SyncChanges() method won’t be invoked. 
       return false; 
      } 
     } 


     /// <summary> 
     /// Reads current value from parent web part and show that in the ddl 
     /// </summary> 
     public override void SyncChanges() 
     { 
      EnsureChildControls(); 
      WebPartPerso ParentWebPart = (WebPartPerso)WebPartToEdit; 

      if (ParentWebPart != null) 
      { 

       AppChoices.Text = ParentWebPart.MyAppChoice; 
       PortalChoices.Text = ParentWebPart.MyPortalChoice; 
       ListViews.Text = ParentWebPart.MyViewChoice.ToString(); 


      } 
     } 
    } 

ответ

0

Сделать WebBrowsable правды

[WebBrowsable(true), Personalizable(PersonalizationScope.User), DefaultValue("PERSO")] 
    public string MyPortalChoice { get; set; } 
+0

Я использую пользовательский EditorPart я не хочу использовать по умолчанию WebPart Свойства. Атрибут: [WebBrowable (false)] здесь, чтобы скрыть атрибут по умолчанию из панели WebPart, чтобы избежать ошибок конечного пользователя. –

Смежные вопросы