簡體   English   中英

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

[英]C# Modify items in drop down list

我的應用程序當前在帶有 FormAuthentication 的 WebForms 應用程序中使用成員資格和角色。

我目前的角色是:
行政
來賓
用戶

一些用戶已創建但從未分配過角色,因為它是直接在數據庫中完成的(在我了解成員資格和角色如何工作之前)。

所有分配了角色的用戶都是“用戶”。 在數據庫的 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. 我的下拉列表中無法顯示“無”。

我希望用戶僅 select 無、管理員或用戶。
我不會讓 None 被保存 - 這將給出一個必須選擇不同角色的錯誤。
我將檢查當前的下拉選擇是否與他們所在的角色匹配。

如果有變化,那么:
對於管理員,我將為“管理員”添加角色。
對於用戶,我將檢查他們是否是管理員和 RemoveRole("Admin") 如果他們是。
對於沒有角色的用戶(currentUserRoles 為空),我將添加一個“用戶”角色。

我使用 Guest 作為角色以只讀方式瀏覽該站點。 我不希望這是一個選擇。

此外,大多數下拉菜單都會有一個鍵/項目和值。 我從未將角色分配給下拉列表,所以我是否只在執行“RoleDDL.datasource = rolesArray”時分配鍵/項目?

我究竟做錯了什么?

我認為您可以嘗試在角色數組中添加 None 並刪除 Guest,而不是在 RoleDDL 中進行。 因為我們可以看到您在 RoleDDL 中所做的修改沒有像您預期的那樣工作。

您也可以嘗試“覆蓋”Guest 角色,因為如果我理解正確,所有具有 Guest 角色的成員都應該擁有 None 角色。 因此,如果您將 Guest 更改為停止顯示 Guest 而是顯示 None,那么您不必更改數據庫的行為而只需更改顯示。

根據MSDNDataBindDataSource綁定到控件(在本例中為DropDownList )。

這意味着任何先前的數據都將被丟棄,來自DataSource的數據將被復制到控件中。

在您的情況下,問題是您在插入和刪除項目后調用DropDownList.DataBind() 要解決您的問題,您需要在更改DropDownList.DataSource后調用DropDownList.DataBind()並在綁定數據后調用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");
// [...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM