using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Cms.Web.UI;
using Telerik.Web.UI;
using System.Web.UI;
using Telerik.Framework.Web;
using Telerik.Security;
namespace Sitefinity.Samples.WebControls
{
class RolesSelector : WebUITypeEditor<string []>
{
public override string[] Value
{
get
{
IList<string> roles = (new string[]{}).ToList();
foreach (RadListBoxItem item in RolesDestination.Items)
{
roles.Add(item.Text.ToString());
}
return roles.ToArray();
}
set
{
this.ViewState["selectedRoles"] = value;
}
}
private string layoutTemplatePath = "~/Sitefinity/Admin/ControlTemplates/Selectors/RolesSelector.ascx";
public string Template
{
get
{
object o = this.ViewState["Template"];
if (o == null)
return this.layoutTemplatePath;
return (string)o;
}
set
{
this.ViewState["Template"] = value;
}
}
protected RadListBox RolesSource
{
get
{
return this.Controls[0].FindControl("RolesSource") as RadListBox;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
userManger = new UserManager();
this.template = ControlUtils.GetTemplate<SelectorTemplate>(Template);
this.template.InstantiateIn(this);
RolesSource.DataSource = userManger.GetAllRoles();
RolesSource.DataBind();
if (this.ViewState["selectedRoles"] != null)
{
IList<string> selectedRoles = (IList<string>)this.ViewState["selectedRoles"];
RolesDestination.DataSource = selectedRoles;
RolesDestination.ItemDataBound += new RadListBoxItemEventHandler(RolesDestination_ItemDataBound);
RolesDestination.DataBind();
}
}
void RolesDestination_ItemDataBound(object sender, RadListBoxItemEventArgs e)
{
RadListBoxItem item = RolesSource.FindItemByText(e.Item.Text);
if (item != null)
{
RolesSource.Delete(item);
}
}
protected RadListBox RolesDestination
{
get
{
return this.Controls[0].FindControl("RolesDestination") as RadListBox;
}
}
public class SelectorTemplate : ITemplate
{
#region ITemplate Members
public void InstantiateIn(Control container)
{
//throw new NotImplementedException();
}
#endregion
}
private ITemplate template;
private UserManager userManger;
}
}