1

i am using membership for the first time for add user roles..Now as per my need i am displaying user roles with their names in columns of gridview as checked or unchecked based on the roles assigned to particular user.. Now i want to upgrade the roles by clicking on checkboxes ..in this case i need to update multiple roles with singme username in membership table...

My concern is that is there any method in membership by which we can update multiple roles with single username ...

Here is my code to get the selected role from gridview column..

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();
}

Any help will be highly appreciated.. Thanks in advance..

4

2 回答 2

1

Add a Button to Update roles outside of GridView.

Try this

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();
    }
}
于 2013-09-20T08:07:33.487 回答
0

You may try this:

Roles.AddUserToRole("username", "rolename") //Add user to one role
Roles.AddUserToRoles("username", rolenames) //Add to roles.  rolenames is string[]
于 2013-09-20T07:42:04.977 回答