⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userroles.aspx.cs

📁 ASP.NET多线程编程(二),ASP.NET多线程编程(二) .
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

public partial class Admin_UserRoles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string userName = Request.QueryString["userName"];

            string[] userRoles = Roles.GetRolesForUser(userName);
            string[] availableRoles = Roles.GetAllRoles();

            StringCollection filter = new StringCollection();
            filter.AddRange(availableRoles);

            foreach (string role in userRoles)
            {
                if (filter.Contains(role))
                {
                    filter.Remove(role);
                }
            }

            availableRoles = new string[filter.Count];

            filter.CopyTo(availableRoles, 0);

            listAvailableRoles.DataSource = availableRoles;
            listAvailableRoles.DataBind();

            listAssignedRoles.DataSource = userRoles;
            listAssignedRoles.DataBind();
        }

    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string userName = Request.QueryString["userName"];
        //Create a container for the users current roles
        StringCollection existingRoles = new StringCollection();
        existingRoles.AddRange(Roles.GetRolesForUser(userName));

        //These are roles a user was in previously, but has been removed now.
        foreach (string role in existingRoles)
        {
            Roles.RemoveUserFromRole(userName, role);
        }

        //loop through users assigned roles
        foreach (ListItem li in listAssignedRoles.Items)
        {
            //This role is not in the existing roles collection, so we add it
            Roles.AddUserToRole(userName, li.Text);
        }

        Response.Redirect("members.aspx");

    }
    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        SwapListItem(listAvailableRoles, listAssignedRoles);
        listAvailableRoles.SelectedIndex = -1;
        listAssignedRoles.SelectedIndex = -1;

    }
    protected void btnRemoveRole_Click(object sender, EventArgs e)
    {
        SwapListItem(listAssignedRoles, listAvailableRoles);
        listAvailableRoles.SelectedIndex = -1;
        listAssignedRoles.SelectedIndex = -1;
    }


    protected void SwapListItem(ListBox source, ListBox destination)
    {
        ListItem item = source.SelectedItem;

        if (item != null)
        {
            destination.Items.Add(item);
            source.Items.Remove(item);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -