I am using VS2005 C#.
I have a list of users and 2 checkboxes. The checkboxes each indicate Administrator and User. When a user is selected, the respective checkboxes will be checked according to the roles assigned to the user.
Now I am trying to add/remove roles from the selected user by checking/unchecking the checkboxes but I am having some problems. The page reloads for a moment and resets the checkbox status back to the original, and the role is not changed.
Below is my code snippets:
<asp:CheckBox ID="adminCB" runat="server" Text="Administrator" OnCheckedChanged="Admin_CC" AutoPostBack="true"/><br />
<asp:CheckBox ID="userCB" runat="server" Text="User" OnCheckedChanged="User_CC" AutoPostBack="true"/>
protected void Admin_CC(Object sender, EventArgs e)
{
if (adminCB.Checked)
{
Roles.AddUserToRole(UsersListBox3.SelectedItem.Value, "Administrator");
// Re-bind users in role to GridView.
usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
UsersInRoleGrid.DataSource = usersInRole;
UsersInRoleGrid.DataBind();
}
if (adminCB==null)
{
Roles.RemoveUserFromRole(UsersListBox3.SelectedItem.Value, "Administrator");
// Re-bind users in role to GridView.
usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
UsersInRoleGrid.DataSource = usersInRole;
UsersInRoleGrid.DataBind();
}
}
Anyone can spot my mistake for me?
Place the check box buttons in an update panel.