ASP.Net 成员资格:在列表框中列出角色

发布于 2024-12-28 22:24:40 字数 479 浏览 0 评论 0原文

我正在尝试整合一个自定义用户更新页面,以便您可以在一个页面上更新用户的电子邮件、密码和角色。我被角色困住了。我已经列出了角色,但我想在列表框中选择分配给用户的角色。

在这种情况下,每个用户只有一个角色。

我尝试过使用 Roles.GetRolesForUser("userName"); 但运气不佳。

这是列表框代码:

<asp:ListBox ID="myRoles" runat="server"
     SelectionMode="Single" >
</asp:ListBox>

这是列表框的数据绑定:

rolesArray = Roles.GetAllRoles();
myRoles.DataSource = rolesArray;
myRoles.DataBind();

最简单的方法是什么?

I'm trying to put together a custom user update page so you can update a user's email, password and role all on one page. I'm stuck on the roles. I have listed the roles ok, but I want to select the role allocated to the user in a listbox.

In this case each user will only have one role.

I've tried playing around with Roles.GetRolesForUser("userName"); without much luck.

Here's the listbox code:

<asp:ListBox ID="myRoles" runat="server"
     SelectionMode="Single" >
</asp:ListBox>

And here's the databinding for the Listbox:

rolesArray = Roles.GetAllRoles();
myRoles.DataSource = rolesArray;
myRoles.DataBind();

What's the easiest to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2025-01-04 22:24:40
protected void Page_Load(object sender, EventArgs e)
    {
        var allRoles = new[] {"Admin", "Client", "Super Admin", "Other"};

        //Returns string array
        var rolesByUser = Roles.GetRolesForUser(HttpContext.User.Identity.Name);

        myRoles.DataSource = allRoles;
        myRoles.DataBind();

        foreach (ListItem role in myRoles.Items)
        {
            foreach (var userRole in rolesByUser)
            {
                if (role.Text == userRole)
                    role.Selected = true;
            }
        }
    }

然后在您的 html 中:

<asp:ListBox ID="myRoles" SelectionMode="Multiple" runat="server"></asp:ListBox>

Single Role/Single Selection Only

将循环更改为:

foreach (ListItem role in myRoles.Items)
        {
            if (role.Text == rolesByUser.FirstOrDefault())
                role.Selected = true;
        }

替代方案

这是使用 LINQ to Objects 的循环的较短版本:

    foreach (ListItem role in myRoles.Items)
            {
                foreach (var userRole in rolesByUser
                                    .Where(userRole => role.Text == userRole))
                {
                    role.Selected = true;
                }
            }

最后是纯 LINQ 模式,在我看来,它有点难以阅读:

foreach (ListItem role in from ListItem role in myRoles.Items 
                from userRole in rolesByUser.Where(userRole => role.Text == userRole) 
                select role)
        {
            role.Selected = true;
        }
protected void Page_Load(object sender, EventArgs e)
    {
        var allRoles = new[] {"Admin", "Client", "Super Admin", "Other"};

        //Returns string array
        var rolesByUser = Roles.GetRolesForUser(HttpContext.User.Identity.Name);

        myRoles.DataSource = allRoles;
        myRoles.DataBind();

        foreach (ListItem role in myRoles.Items)
        {
            foreach (var userRole in rolesByUser)
            {
                if (role.Text == userRole)
                    role.Selected = true;
            }
        }
    }

Then in your html:

<asp:ListBox ID="myRoles" SelectionMode="Multiple" runat="server"></asp:ListBox>

Single Role/Single Selection Only

Change loop to :

foreach (ListItem role in myRoles.Items)
        {
            if (role.Text == rolesByUser.FirstOrDefault())
                role.Selected = true;
        }

Alternatives

Here's a shorter version of that loop using LINQ to Objects:

    foreach (ListItem role in myRoles.Items)
            {
                foreach (var userRole in rolesByUser
                                    .Where(userRole => role.Text == userRole))
                {
                    role.Selected = true;
                }
            }

And finally a pure LINQ mode that is in my opinion a bit too hard to read:

foreach (ListItem role in from ListItem role in myRoles.Items 
                from userRole in rolesByUser.Where(userRole => role.Text == userRole) 
                select role)
        {
            role.Selected = true;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文