3-7 SharePoint 自定义的EidtorPart

新建 EPForDemo 类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;


namespace ljzEditorPart
{
    class EPForDemo:EditorPart
    {
        CheckBox[] cb = new CheckBox[10];
        Button btn = new Button();
        string type = "头条新闻;本地新闻;财经新闻;娱乐新闻;体育新闻;社会新闻";
        protected override void CreateChildControls()
        {
            string[] type2 = type.Split(';');
            int typecount = type2.Length;
            Table table = new Table();
            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.Style.Add(HtmlTextWriterStyle.Height, "190px");
            table.Style.Add(HtmlTextWriterStyle.Width, "200px");
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            for (int i = 0; i < type2.Length; i++)
            {
                cb[i] = new CheckBox();
                cb[i].Text = type2[i];
                this.Controls.Add(cb[i]);
                row = new TableRow();
                cell1 = new TableCell();
                cell2 = new TableCell();
                cell1.Style.Add(HtmlTextWriterStyle.TextAlign, "left");
                cell1.Controls.Add(cb[i]);
                row.Cells.Add(cell1);
                table.Rows.Add(row);
            }

            btn.Click += Btn_Click;
            btn.Text = "全选";
            this.Controls.Add(btn);
            row = new TableRow();
            cell1 = new TableCell();
            cell2 = new TableCell();
            cell1.Style.Add(HtmlTextWriterStyle.TextAlign, "left");
            cell1.Controls.Add(btn);
            row.Cells.Add(cell1);
            table.Rows.Add(row);
            this.Controls.Add(table);

            // base.CreateChildControls();
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < type.Split(';').Length; i++)
            {
                cb[i].Checked = true;
            }
            //throw new NotImplementedException();
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
           WPForDemo.WPForDemo webpart = this.WebPartToEdit as WPForDemo.WPForDemo;
            if (webpart == null) return false;
            webpart.Test = "";
            for (int i = 0; i < type.Split(';').Length; i++)
            {
                if (this.cb[i].Checked == true && this.cb[i] != null)
                {
                    webpart.Test += "1;";
                }
                else
                {
                    webpart.Test += "0;";
                }
            }
            return true;

            //throw new NotImplementedException();
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            WPForDemo.WPForDemo webpart = this.WebPartToEdit as WPForDemo.WPForDemo;
            if (webpart == null) return;
            string GetTest = webpart.Test;
            string[] GetTestGroup = GetTest.Split(';');

            for (int i = 0; i < GetTestGroup.Length; i++)
            {
                if (GetTestGroup[i] == "1")
                {
                    cb[i].Checked = true;
                }
            }

            //throw new NotImplementedException();
        }
    }
}

添加webpart部件进行关联

 

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

namespace ljzEditorPart.WPForDemo
{
    [ToolboxItemAttribute(false)]
    public class WPForDemo : WebPart
    {
        private string _Test = "";
        [WebBrowsable(true)]
        [WebDisplayName("类别")]
        [WebDescription("请输入类别...")]
        [Category("设置")]
        [Personalizable(true)]
        public string Test
        {
            get
            {
                return _Test;
            }
            set
            {
                _Test = value;
            }
        }

        public override EditorPartCollection CreateEditorParts()
        {
            EditorPartCollection baseParts = base.CreateEditorParts();
            List<EditorPart> editorParts = new List<EditorPart>(1);
            EditorPart part = new EPForDemo();
            part.ID = this.ID + "_tagValueEditor";
            part.Title = "新闻类别";
            editorParts.Add(part);
            return new EditorPartCollection(baseParts, editorParts);
            //return base.CreateEditorParts();
        }

    }
}

演示:

 

 

 

posted @ 2020-05-28 17:40  七秒钟得记忆  阅读(103)  评论(0)    收藏  举报