Set default role to new user
Hi all,
Is it possible to set a default role when creating a new user through the backend interface?
I need to have a particular role being checked, by default.
Best,
Daniel
Hello Daniel,
You should map the UserCreate view and manipulate the RolesRepeater which holds the Roles and the Checkbox for each role selection. Using the codebehind of the mapped view, we can iterate the roles collection and find the one we need, then find the underlying checkbox and check it:
public
partial
class
UserNew : System.Web.UI.UserControl
protected
void
Page_Load(
object
sender, EventArgs e)
var rolesList =
this
.FindControl(
"rptRolesList"
)
as
Repeater;
if
(rolesList !=
null
)
var items = rolesList.DataSource
as
List<RoleProviderPair>;
if
(items !=
null
&& items.Count > 0)
for
(
int
i = 0; i < items.Count; i++)
if
(items[i].RoleItem.Name ==
"Authors"
)
var checkBox = rolesList.Items[i].FindControl(
"role"
)
as
CheckBox;
if
(checkBox !=
null
)
checkBox.Checked =
true
;
break
;