WebPart属性持久性

时间:2016-02-28 08:08:00

标签: sharepoint sharepoint-2013 web-parts

我尝试开发一个Web部件,显示在自定义EditorPart的属性中输入的文本, 但我有属性持久性问题当我单击确定或保存并打开WebPart属性时,属性的值恢复为默认值, 在编辑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();


            }
        }
   }

1 个答案:

答案 0 :(得分:0)

WebBrowsable设为

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