简体   繁体   中英

C# Modify items in drop down list

My application currently uses Membership and Roles in a WebForms app with FormAuthentication.

My current roles are:
Admin
Guest
Users

Some users were made but never got a role assigned as it was done directly in the database (before I understand how Membership and Roles worked).

All users are that have a role assigned are "Users". All Admin's have both an "Admin" role and a "Users" role in the UsersInRoles table in the database.

The code that follows is after I have clicked on a user from a user list datagrid on a different page. The username is passed via url parameter.

I have the following code:

public partial class editUser : System.Web.UI.Page
    {
        public MembershipUser usr;
        string[] rolesArray;
        public string roleChoice;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.QueryString["username"] != null)
            {
                usr = Membership.GetUser(Request.QueryString["username"]);
            } else
            {
                // no user passed, so default to current user
                usr = Membership.GetUser(User.Identity.Name);  
            }

            if (!Page.IsPostBack)
            {

                // Get the list of roles and populate dropdown
                rolesArray = System.Web.Security.Roles.GetAllRoles();

                RoleDDL.DataSource = rolesArray;
                // add a "None" role to anyone that doesn't have a role assigned yet
                RoleDDL.Items.Insert(0, new ListItem("None"));
                // do not allow a user to be assigned the "Guest" role.
                RoleDDL.Items.Remove("Guest");
                RoleDDL.DataBind();

                // Set current email on page load for the user.  If username was passed then that user, otherwise the user that is logged in as an Admin
                EmailTextBox.Text = usr.Email;
                IsLockedOutCheckbox.Checked = usr.IsLockedOut;
                IsApprovedCheckbox.Checked = usr.IsApproved;

                // If the user is not locked out, disable the checkbox because you cannot set the checkbox programatically
                if (IsLockedOutCheckbox.Checked == false) 
                {
                    IsLockedOutCheckbox.Enabled = false;
                }

                // Select current role for user
                string[] currentUserRoles = System.Web.Security.Roles.GetRolesForUser(usr.UserName);
                // Choose the current user role from currentUserRoles and assign to DDL.  Always choose Admin if it exists, User only if Admin doesn't exist, and None if no roles.
                foreach (string value in currentUserRoles) {
                    if (System.Web.Security.Roles.IsUserInRole(usr.UserName, "Admin")) 
                    {
                        RoleDDL.SelectedValue = "Admin";
                        break;  // "Admin" users are also "Users" users so break out of the for loop.  Otherwise the drop down will select "Users".
                    }

                    if (System.Web.Security.Roles.IsUserInRole(usr.UserName, "Users"))
                    {
                        RoleDDL.SelectedValue = "Users";
                    }
                    else
                    {
                        RoleDDL.SelectedValue = "None";
                    }

                }
            }

        }
}

The drop down list after running my code is as follows:

Admin
Guest
Users

My issues are:

  1. Removing "Guest" is failing and still shows in my drop down list.
  2. I cannot get "None" to show up in my drop down list.

I want the user to only select None, Admin, or User.
I won't let None be saved - this will give an error that a different role must be chosen.
I will check if the current drop down selection matches the roles they are in.

If there is a change then:
For Admin I will add the role for "Admin".
For User I will check if they were an admin and RemoveRole("Admin") if they were.
For users that do not have a role (currentUserRoles is null) i'll add a "Users" role.

I am using Guest as a role to browse the site as read-only. I do not want this as a choice.

Also most drop downs will have an key/item and value. I've never assigned roles to a dropdown list, so am I only assigning the key/item when I do "RoleDDL.datasource = rolesArray"?

What am I doing wrong?

I think you could try to add None and also remove Guest in your rolesArray instead of doing it in the RoleDDL. Because we can see that the modification that you are doing in the RoleDDL are not working as you were expecting it.

You could also try to "overwrite" the Guest role, because, if I understand correctly, all members with the Guest role should instead have the None role. So if you change the Guest to stop showing Guest but instead show None, than you wouldn't have to change the behaviour of your database but only change the display.

According to MSDN , DataBind binds the DataSource to the control ( DropDownList in this case).

This means that any prior data will be discarded and the data from DataSource will be copied in the control.

In your case, the issue is that you call DropDownList.DataBind() after you insert and remove items. To fix your issue, you need call DropDownList.DataBind() after changing the DropDownList.DataSource and call the Items.Insert and Items.Remove after the data has been bound. Both of these methods will do the binding on their own.

// [...]
RoleDDL.DataSource = rolesArray;
// Bind the DataSource
RoleDDL.DataBind(); 
// Both of these will perform the binding on their own.
RoleDDL.Items.Insert(0, new ListItem("None"));
RoleDDL.Items.Remove("Guest");
// [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM