C# 修改下拉列表中的项目

问题描述

我的应用程序目前在具有 FormAuthentication 的 WebForms 应用程序中使用 Membership 和 Roles。

我目前的角色是:
管理员
客人
用户

有些用户是创建的,但从未分配过角色,因为它是直接在数据库中完成的(在我了解成员资格和角色的工作原理之前)。

所有分配了角色的用户都是“用户”。在数据库的 UsersInRoles 表中,所有管理员都具有“管理员”角色和“用户”角色。

接下来的代码是在我从不同页面上的用户列表数据网格中单击用户之后。用户名通过 url 参数传递。

我有以下代码

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";
                    }

                }
            }

        }
}

运行我的代码后的下拉列表如下:

管理员
客人
用户

我的问题是:

  1. 删除“访客”失败,但仍显示在我的下拉列表中。
  2. 我的下拉列表中无法显示“无”。

我希望用户只选择无、管理员用户
我不会让 None 被保存 - 这将给出一个错误,必须选择不同的角色。
我会检查当前的下拉选项是否与他们所处的角色匹配。

如果有变化,则:
对于管理员,我将为“管理员添加角色。
对于用户,我会检查他们是否是管理员,如果他们是,我会检查 RemoveRole("Admin")。
对于没有角色的用户(currentUserRoles 为空),我将添加一个用户”角色。

我使用 Guest 作为角色以只读方式浏览网站。我不希望这是一个选择。

此外,大多数下拉菜单都有一个键/项目和值。我从未将角色分配给下拉列表,所以我是否只在执行“RoleDDL.datasource = rolesArray”时分配键/项目?

我做错了什么?

解决方法

我认为您可以尝试在您的角色数组中添加 None 并删除 Guest,而不是在 RoleDDL 中执行此操作。因为我们可以看到您在 RoleDDL 中所做的修改没有按您的预期工作。

您也可以尝试“覆盖”Guest 角色,因为如果我理解正确的话,所有具有Guest 角色的成员都应该拥有None 角色。因此,如果您将 Guest 更改为停止显示 Guest 而是显示 None,那么您就不必更改数据库的行为,而只需更改显示。

,

根据MSDNDataBindDataSource 绑定到控件(在本例中为DropDownList)。

这意味着任何先前的数据都将被丢弃,DataSource 中的数据将被复制到控件中。

就您而言,问题在于您在插入和删除项目后调用 DropDownList.DataBind()。要解决您的问题,您需要在更改 DropDownList.DataBind() 后调用 DropDownList.DataSource,并在绑定数据后调用 Items.InsertItems.Remove。这两种方法都会自己进行绑定。

// [...]
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");
// [...]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...