PeopleEditor control and the multiview
May 29, 2008 Leave a comment
I spend quite some hours on this one. Here is my configuration:
– a web part with a multiview control with several views in it
– a few linkbuttons to navigate between views
– one of these views contains SharePoint’s PeopleEditor control
– the web part runs in a SharePoint site
Now, the problem was that the PeopleEditor does not retain its values after more than 2 postbacks. As the multiview triggers postbacks, this cannot be accepted. I searched a lot on the internet, but not much information here.
So, i needed a workaround, which i found by subclassing the PeopleEditor control, and override the "RaisePostDataChangedEvent" and the "OnPreRender" event. In short; i store the selected values in a Session var, and use this same session var to load it in the OnPreRender event. I have chosen Sesson vars instead of ViewState because now, I can access it from anywhere in my application, but ViewState works as well.
Here is the code for the PeopleEditor subclass:
================================================================================
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Collections;
using System.Web.SessionState;
namespace wpPeopleEditor
{
public class myPPE: PeopleEditor
{
public override void RaisePostDataChangedEvent()
{
Context.Session.Remove("sessionvalues");
Context.Session.Add("sessionvalues", this.CommaSeparatedAccounts);
}
protected override void OnPreRender(EventArgs e)
{
this.CommaSeparatedAccounts=(String)Context.Session["sessionvalues"];
base.OnPreRender(e);
}
}
}
================================================================================
And, for the fun of it, the code of the web part itself:
================================================================================
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Collections;
namespace wpPeopleEditor
{
[Guid("e377c0db-7e7b-430b-9935-aa482eb25cd3")]
public class wpPeopleEditor : System.Web.UI.WebControls.WebParts.WebPart
{
public MultiView mv;
public View vw1;
public View vw2;
public View vw3;
public myPPE PeopleEditor1;
public wpPeopleEditor()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
this.EnsureChildControls();
RenderChildren(writer);
}
protected override void CreateChildControls()
{
mv = new MultiView();
vw1 = new View();
Label lbl1 = new Label();
lbl1.Text = "View1";
vw1.Controls.Add(lbl1);
mv.Views.Add(vw1);
vw2 = new View();
PeopleEditor1 = new myPPE();
PeopleEditor1.AutoPostBack = true;
PeopleEditor1.PlaceButtonsUnderEntityEditor = true;
PeopleEditor1.ID = "pplEditor";
PeopleEditor1.AllowEmpty = false;
PeopleEditor1.SelectionSet = "User,DL,SecGroup,SPGroup";
PeopleEditor1.MultiSelect = true;
PeopleEditor1.EnableViewState = true;
vw2.Controls.Add(PeopleEditor1);
mv.Views.Add(vw2);
vw3 = new View();
Label lbl3 = new Label();
lbl3.Text = "View3";
vw3.Controls.Add(lbl3);
mv.Views.Add(vw3);
this.Controls.Add(mv);
this.Controls.Add(new LiteralControl("<br>"));
LinkButton btn1 = new LinkButton();
btn1.Text = "view 1";
btn1.CausesValidation = true;
btn1.Click += new EventHandler(btn1_Click);
this.Controls.Add(btn1);
LinkButton btn2 = new LinkButton();
btn2.Text = "view 2" + PeopleEditor1.CommaSeparatedAccounts;
btn2.CausesValidation = true;
btn2.Click += new EventHandler(btn2_Click);
this.Controls.Add(btn2);
LinkButton btn3 = new LinkButton();
btn3.Text = "view 3: " + PeopleEditor1.CommaSeparatedAccounts;
btn3.CausesValidation = true;
btn3.Click += new EventHandler(btn3_Click);
this.Controls.Add(btn3);
base.CreateChildControls();
}
void btn1_Click(object sender, EventArgs e)
{
mv.SetActiveView(vw1);
}
void btn2_Click(object sender, EventArgs e)
{
mv.SetActiveView(vw2);
}
void btn3_Click(object sender, EventArgs e)
{
mv.SetActiveView(vw3);
}
}
}
================================================================================
As you can see, everything is neat and clean in the MyPeopleEditor class itself; no need to get/set stuff in the parent control.
Have fun with it!