The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
<%@ Control Language="C#" %>
<
p
><
h2
>Select roles which will see the control's content.</
h2
></
p
>
<
asp:Label
AssociatedControlID
=
"RolesSource"
runat
=
"server"
ID
=
"label1"
Text
=
"Roles to select from:"
></
asp:Label
><
br
/>
<
telerik:RadListBox
runat
=
"server"
ID
=
"RolesSource"
Height
=
"200px"
Width
=
"200px"
AllowTransfer
=
"true"
TransferToID
=
"RolesDestination"
>
</
telerik:RadListBox
>
<
telerik:RadListBox
runat
=
"server"
ID
=
"RolesDestination"
Height
=
"200px"
Width
=
"200px"
/>
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;
}
}
/// <summary>
/// Gets or sets the roles which are allowed to see content of the cotnrol. We are going to use the RolesSelector for WebEditor
/// </summary>
[Browsable(
true
)]
[WebEditor(
"Sitefinity.Samples.WebControls.RolesSelector, Sitefinity.Samples"
),TypeConverter(
typeof
(Telerik.Framework.Utilities.StringArrayConverter))]
public
string
[] Roles
{
get
{
object
obj =
this
.roles;
if
(obj ==
null
)
return
new
string
[]{};
return
(
string
[])obj;
}
set
{
this
.roles = value;
}
}
/// <summary>
/// The div containing the generic content control
/// </summary>
[Browsable(
false
)]
public
HtmlGenericControl ContentWrapper
{
get
{
return
this
.Container.GetControl<HtmlGenericControl>(
"contentWrapper"
,
true
); }
}
protected
override
void
Render(HtmlTextWriter writer)
{
StringWriter content =
null
;
HtmlTextWriter contentWriter =
null
;
try
{
// if in normal mode, output the full content of the control
if
(!
this
.DesignMode)
{
// first, we need to "extract"
content =
new
StringWriter();
contentWriter =
new
HtmlTextWriter(content);
base
.Render(contentWriter);
this
.ContentPlaceholder.Text = content.ToString();
//check selected roles and set behavior accordingly
if
(
this
.roles !=
null
)
{
UserManager userManager =
new
UserManager();
MembershipUser currentUser = userManager.GetUser();
if
(currentUser !=
null
)
{
bool
userInRole =
false
;
foreach
(
string
role
in
this
.roles)
{
userInRole = userManager.IsUserInRole(role);
if
(userInRole ==
true
)
break
;
}
if
(userInRole ==
false
)
ContentWrapper.Visible =
false
;
}
else
{
ContentWrapper.Visible =
false
;
}
}
// we need to do this manually, as generic content control renders its
// content directly and does not inherit CompositeControl,
// so it will not render out template correctly (not at all, actually)
foreach
(Control child
in
this
.Controls)
{
child.RenderControl(writer);
}
if
(!
this
.Controls.Contains(
this
.Container))
{
this
.Container.RenderControl(writer);
}
}
else
{
// if in edit (design) mode, output the content of the genreric content control
// THIS IS REQUIRED
base
.Render(writer);
}
}
finally
{
if
(content !=
null
)
{
content.Dispose();
}
if
(contentWriter !=
null
)
{
contentWriter.Dispose();
}
}
}
<
toolboxControls
>
<
clear
/>
...
<
add
name
=
"Custom Generic Content"
section
=
"Sitefinity Samples"
type
=
"Sitefinity.Samples.WebControls.GCWrapper, Sitefinity.Samples"
/>
</
toolboxControls
>
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.