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

📄 manageusers.ascx.cs

📁 学生成绩管理系统
💻 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;

public partial class TeachHelper_Controls_ManageUsers : System.Web.UI.UserControl
{

    string[] rolesArray;

    #region 事件处理

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Bind roles to GridView.

            rolesArray = Roles.GetAllRoles();
            this.CheckBoxListRoles.DataSource = rolesArray;
            this.CheckBoxListRoles.DataBind();
        }
    }

    protected void ButtonFindUser_Click(object sender, EventArgs e)
    {
        FindUserAndBind();
    }

    protected void GridViewUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
            //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"" + "#add816" + "\"");
            ((ImageButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
        }
    }

    protected void GridViewUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridViewUser.Rows[index];
        string username = Server.HtmlDecode(row.Cells[0].Text);   

        if (e.CommandName == "DeleteUser")
        {
            Membership.DeleteUser(username);
        }

        if (e.CommandName == "EditRoles")
        {
            this.CheckBoxListRoles.Visible = true;
            this.LabelUser.Visible = true;
            this.LabelUserValue.Visible = true;

            this.LabelUserValue.Text = username;
            SetCheckBoxListRoles(username);
        }

    }

    protected void GridViewUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridViewUser.PageIndex = e.NewPageIndex;
        FindUserAndBind();
    }

    protected void CheckBoxListRoles_SelectedIndexChanged(object sender, EventArgs e)
    {
        EditRoles();
    }

    #endregion

    #region 私有 & 保护方法

    private void SetCheckBoxListRoles(string username)
    {
        string[] userRolesArray = Roles.GetRolesForUser(username);

        foreach (ListItem li in this.CheckBoxListRoles.Items)
        {
            li.Selected = false;
        }

        foreach (string role in userRolesArray)
        {
            foreach (ListItem li in this.CheckBoxListRoles.Items)
            {
                if (li.Text == role)
                {
                    li.Selected = true;
                }
            }
        }
    }

    private void EditRoles()
    {
        string username = this.LabelUserValue.Text;
        string roleName;

        foreach (ListItem li in this.CheckBoxListRoles.Items)
        {
            roleName = li.Text;
            if (li.Selected)
            {
                if (!Roles.IsUserInRole(username, roleName))
                {
                    Roles.AddUserToRole(username, roleName);
                }
            }
            else
            {
                if (Roles.IsUserInRole(username, roleName))
                {
                    Roles.RemoveUserFromRole(username, roleName);
                }
            }
        }
    }

    private void FindUserAndBind()
    {
        MembershipUserCollection meberCollection = new MembershipUserCollection();

        if (this.DropDownListFindCondition.SelectedValue == "用户名")
        {
            meberCollection = Membership.FindUsersByName(this.TextBoxKey.Text);
        }
        else
        {
            meberCollection = Membership.FindUsersByEmail(this.TextBoxKey.Text);
        }

        if (meberCollection.Count == 0)
        {
            this.LabelMessage.Text = "没有您要搜索的用户。";
            this.Panel1.Visible = false;
        }
        else
        {
            this.GridViewUser.DataSource = meberCollection;
            this.GridViewUser.DataBind();
            this.LabelMessage.Text = string.Empty;
            this.Panel1.Visible = true;
        }
    }

    #endregion

}

⌨️ 快捷键说明

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