创建角色,以及编辑删除角色

Web.config 中的设置
<configuration >
    <connectionStrings>
        <add name="SqlServices" connectionString="Data Source=WANGXIAOJUN;Initial Catalog=aspnetdb;Integrated Security=True"
            />
</connectionStrings>

<authentication mode="Forms">
      <forms loginUrl="Default.aspx"></forms>
</authentication>


<roleManager enabled="true" defaultProvider="SqlProvider" >
        <providers>
          <add name="SqlProvider" type="System.Web.Security.SqlRoleProvider"
               connectionStringName="SqlServices"/>
        </providers>
  </roleManager>



AddRole.aspx 页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="addroles.aspx.cs" Inherits="addroles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="0" cellpadding="2" cellspacing="2" class="main">
            <tr align="center" class="head">
                <td><b>角色管理</b></td> 
            </tr>
            <tr>
                <td align="center">
                    <asp:TextBox ID="txtRole" runat="server" Width="120px"></asp:TextBox>
                    &nbsp &nbsp
                    <asp:Button ID="btnSubmit" runat="server" Text="确定" OnClick="btnSubmit_Click"/>
                    <hr />
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" GridLines="None"
                    Font-Size="small" Width="204px">
                    <Columns>
                        <asp:TemplateField HeaderText="角色名">
                            <ItemTemplate>
                                <asp:Label ID="lbRoleName" runat="server" ForeColor="black"
                                 Text='<%# Container.DataItem.ToString() %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle Height="25px" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lbEdit" runat="server" Text="编辑角色"
                                 CommandName="EditRole" CommandArgument='<%# Container.DataItem.ToString() %>'
                                  OnCommand="LinkButtonClick"
ForeColor="blue"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lbDel" runat="server" Text="删除" CommandName="DeleteRole"
                                CommandArgument='<%# Container.DataItem.ToString() %>' OnCommand="LinkButtonClick"
                                ForeColor="blue" OnClientClick="return confirm('确定删除该角色吗?');"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>   
                    </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <asp:Panel ID="plUsers" runat="server" Visible="false">
                <tr>
                    <td align="center">
                        <hr />
                        <asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" Width="100%"
                         GridLines="None" Font-Size="small">
                            <Columns>
                                <asp:TemplateField HeaderText="用户名">
                                    <ItemTemplate>
                                        <asp:Label ID="lbUserName" runat="server" ForeColor="black"
                                         Text='<%# DataBinder.Eval(Container.DataItem, "UserName")%>'>
                                        </asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle Height="25px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="是否属于角色">
                                    <ItemTemplate>
                                        <asp:CheckBox ID="cbUserInRole" runat="server" AutoPostBack="true"
                                         ForeColor="blue" OnCheckedChanged="CheckBox_Click"
                                          ToolTip='<%# DataBinder.Eval(Container.DataItem, "UserName") %>' />
                                    </ItemTemplate>
                                    <HeaderStyle Height="25px" />
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </asp:Panel>
            <tr>
                <td align="center">
                    <asp:Label ID="lbMessage" runat="server" ForeColor="red">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td class="Head" align="right" style="Height: 23px">
                <a href="adduser.aspx">为用户设置角色</a>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

addroles.aspx.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 addroles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = Roles.GetAllRoles();
            GridView1.DataBind();
            gvUsers.DataSource = Membership.GetAllUsers();
            gvUsers.DataBind();
        }
    }

    protected void LinkButtonClick(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Equals("EditRole"))
        {
            string rname = e.CommandArgument.ToString();
            gvUsers.Caption = "设置角色<b>" + rname + "</b>的用户";

            for (int i = 0; i < Membership.GetAllUsers().Count; i++)
            {
                CheckBox checkb1 = (CheckBox)gvUsers.Rows[i].FindControl("cbUserInRole");
                string uname = checkb1.ToolTip;
                checkb1.Checked = Roles.IsUserInRole(uname, rname);
                checkb1.Attributes["role"] = rname;
            }

            plUsers.Visible = true;
        }

        if (e.CommandName.Equals("DeleteRole"))
        {
            string deleter = e.CommandArgument.ToString();
            int tleng = Roles.GetUsersInRole(deleter).Length;

            if (tleng != 0)
            {
                lbMessage.Text = "该角色中还包含用户,不能删除该角色";
                return;
            }

            if (Roles.DeleteRole(deleter))
            {
                GridView1.DataSource = Roles.GetAllRoles();
                GridView1.DataBind();
                lbMessage.Text = "删除成功";
            }
            else
            {
                lbMessage.Text = "删除失败";
            }
        }
    }//LinkButtonClick

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string createRole = txtRole.Text;

        if (txtRole.Text == string.Empty)
        {
            lbMessage.Text = "请输入角色名";
            return;
        }

        if (!Roles.RoleExists(createRole))
        {
            Roles.CreateRole(createRole);
            GridView1.DataSource = Roles.GetAllRoles();
            GridView1.DataBind();
            lbMessage.Text = "添加成功";
        }
        else
        {
            lbMessage.Text = "角色存在,请重新输入";
        }
    }//btnSubmit_Click

    protected void CheckBox_Click(object sender, EventArgs e)
    {
        try
        {
            CheckBox cbUserInRole = (CheckBox)sender;
            string uname = cbUserInRole.ToolTip;
            string rname = cbUserInRole.Attributes["Role"];

            if (!cbUserInRole.Checked)
            {
                Roles.RemoveUserFromRole(uname, rname);

            }
            else
            {
                Roles.AddUserToRole(uname, rname);
            }

            lbMessage.Text = "更新成功";
        }//try
        catch(System.Configuration.Provider.ProviderException ex)
        {
            lbMessage.Text = ex.Message;
        }

    }
  
}


posted @ 2008-06-15 16:22  不染丹心  阅读(416)  评论(0编辑  收藏  举报