locked
i am trying to edit the roles of user in membership! RRS feed

  • Question

  • User-1101529841 posted

    Hi,

    I have this code but it is not working. can someone help

     protected void BindGridviewData()
    {
        DataTable dTable = new DataTable();
        dTable.Columns.Add("Select", typeof(bool));
        dTable.Columns.Add("Username", typeof(string));
        dTable.Columns.Add("Role(Admin)", typeof(bool));
        dTable.Columns.Add("Role(Controller)", typeof(bool));
        dTable.Columns.Add("Role(User)", typeof(bool));
        foreach (MembershipUser u in Membership.GetAllUsers())
        {
            DataRow dRow = dTable.NewRow();
            dRow[0] = false;
            dRow[1] = u.UserName;
            string[] roles = Roles.GetRolesForUser(u.UserName);
            dRow[2] = roles.Contains("Admin") ? true : false;
            dRow[3] = roles.Contains("Controller") ? true : false;
            dRow[4] = roles.Contains("User") ? true : false;
            dTable.Rows.Add(dRow);
        }
        GridView1.DataSource = dTable;
        GridView1.DataBind();
        
    }
    
    protected void cmdUpdateRole_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            string username = row.Cells[1].Text;
            CheckBox chkAdmin = (CheckBox)row.Cells[2].Controls[0];
            CheckBox chkUser = (CheckBox)row.Cells[3].Controls[0];
            CheckBox chkgen = (CheckBox)row.Cells[4].Controls[0];         
            List<string> roles=new List<string>();   
            if (chkAdmin.Checked)
                roles.Add("Admin");
            if (chkUser.Checked)
                roles.Add("Controller");
            if (chkgen.Checked)
                roles.Add("User");
            if (Roles.GetRolesForUser(username).Length > 0)
            {
                Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));
            }
            if (roles.Count > 0)
            {
                Roles.AddUserToRoles(username, roles.ToArray());
            }
            BindGridviewData();
        //    GridView1.Visible = false;
        //    cmdUpdateRole.Visible = false;
        //    DropDownList1.SelectedIndex = 0;
    
        }

    Thanks

    Friday, February 5, 2016 10:52 AM

Answers

  • User-718146471 posted

    Ok, let me replicate your project on my end and I'll see what is going on. Have you tried setting a breakpoint in the code and stepping through it to see what the code does? Maybe the event handler is not getting hit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 4:23 PM
  • User-1101529841 posted

    i was doing something wrong with if(!ispostback).

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 7, 2016 5:35 AM

All replies

  • User-718146471 posted

    Can you provide a bit more detail about what you mean by "not working"? That is the same as going to the mechanic saying my car is broke but not telling them what specifically is broken.

    Friday, February 5, 2016 1:31 PM
  • User-1101529841 posted

    Thanks for the reply,

    I mean it doesn't do anything and the gridview is refreshed (back to original), no updates happens based on selection.

    Thanks. 

    Friday, February 5, 2016 2:12 PM
  • User-718146471 posted

    Ok, let me replicate your project on my end and I'll see what is going on. Have you tried setting a breakpoint in the code and stepping through it to see what the code does? Maybe the event handler is not getting hit.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 4:23 PM
  • User-718146471 posted

    Can you post the source from the aspx page? That would save me some time.

    Friday, February 5, 2016 4:24 PM
  • User-1101529841 posted

    i was doing something wrong with if(!ispostback).

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 7, 2016 5:35 AM
  • User-718146471 posted

    See, that is why we ask for code :) You could have saved yourself hours of agony because we probably would have seen that problem right off.

    Monday, February 8, 2016 12:52 PM