我第一次使用会员资格来添加用户角色。现在根据我的需要,我根据分配给特定用户的角色,在gridview列中显示用户角色,其名称为checked或unchecked。 现在我想通过点击复选框升级角色..在这种情况下,我需要在会员表中使用singme用户名更新多个角色...
我担心的是会员资格中是否有任何方法可以用单个用户名更新多个角色...
这是我从gridview列获取所选角色的代码..
protected void UpgradeSelectedRecords(object sender, EventArgs e)
{
string admin;
string DPAOUser;
string GenUser;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkChild") as CheckBox);
if (chkRow.Checked)
{
string name = (row.Cells[2].FindControl("Label1") as Label).Text;
if ((row.Cells[3].FindControl("chkAdmin") as CheckBox).Checked) {
admin = "Admin";
}
if ((row.Cells[4].FindControl("chkUser") as CheckBox).Checked)
{
DPAOUser = "DPAO User ";
}
if ((row.Cells[5].FindControl("chkgen") as CheckBox).Checked)
{
GenUser = "GeneralUser";
}
}
}
}
BindGridviewData();
}
任何帮助将受到高度赞赏.. 提前谢谢..
答案 0 :(得分:1)
添加一个按钮以更新GridView之外的角色。
试试这个
protected void cmdUpdateRole_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
List<string> roles=new List<string>();
Label username = (Label)row.FindControl("Label1");
CheckBox chkAdmin = (CheckBox)row.FindControl("chkAdmin");
CheckBox chkUser = (CheckBox)row.FindControl("chkUser");
CheckBox chkgen = (CheckBox)row.FindControl("chkgen");
if (chkAdmin.Checked)
roles.Add("Admin");
if (chkUser.Checked)
roles.Add("DPAO User");
if (chkgen.Checked)
roles.Add("GeneralUser");
if (Roles.GetRolesForUser(username.Text).Length > 0)
{
Roles.RemoveUserFromRoles(username.Text, Roles.GetRolesForUser(username.Text));
}
if (roles.Count > 0)
{
Roles.AddUserToRoles(username.Text, roles.ToArray());
}
BindGridviewData();
}
}
答案 1 :(得分:0)
你可以试试这个:
Roles.AddUserToRole("username", "rolename") //Add user to one role
Roles.AddUserToRoles("username", rolenames) //Add to roles. rolenames is string[]