概述
先描述一下需求吧,是这样的,因为要开发一个后台,这个后台有一部分功能是权限管理。也就是可以根据公司现在的部门进行单个的权限分配,也可以给某个人分配不同的权限。权限不同,后台相应的功能也不同。我的解决方案是在数据库里首先建立一张字典表(permits),用来存放每个功能和对应的链接。如图所示:
name:功能名称
url:功能页面对应的链接
belongTo:(因为一级菜单、二级菜单、三级菜单都保存在这个字典里,所以是用这个字段来指示的)也就是对应id主键的。
接下来是一个权限的分组表,权限的分配是以组为单位的,每个组下面可以有一个或者多个成员。
id:主键
group:组名
permissionId:对应权限字典表,形如:1,2,3,4,5(这样也方便进行查询,只要用select .....in....)
接下来是一个人员信息表:
其它的就不多做说明了,其中的group:对应分组表里的id,标明此人所属的组
权限绑定
接下来的问题是权限绑定,这个问题其实是好解决的,我们只要把这三级菜单绑定到页面显示出来就好。从事.net开发的朋友可能第一个就会想到repeater控件来实现。
接下来看一下具体的操作步骤:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<dl>
<dt>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("name") %>'></asp:Label>
<asp:HiddenField ID="HiddenField1" Value='<%#Eval("id") %>' runat="server" />
</dt>
<dd>
<asp:Repeater ID="Repeater3" runat="server" OnItemDataBound="Repeater3_ItemDataBound">
<ItemTemplate>
<dt>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%#Eval("name") %>' AutoPostBack="true"
OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:HiddenField ID="HiddenField2" Value='<%#Eval("id") %>' runat="server" />
<dd>
<asp:CheckBoxList ID="cblMember" runat="server">
</asp:CheckBoxList>
</dd>
</dt>
</ItemTemplate>
</asp:Repeater>
</dd>
</dl>
</ItemTemplate>
</asp:Repeater>
命名不规范的问题,在这里先不谈了,嘻嘻,不过看着真得挺别扭的。
最外层的repeater绑定,也就是第一级菜单的绑定:
/// <summary>
/// 绑定权限。
/// </summary>
private void BindPermission()
{
BLL.Permission_permitsBLL Permissioin_permitsBLL = new BLL.Permission_permitsBLL();
System.Data.DataTable dt = Permissioin_permitsBLL.GetAllPermission();
System.Data.DataView dv = dt.DefaultView;
dv.RowFilter = "belongTo=0";
Repeater1.DataSource = dv;
Repeater1.DataBind();
}
其中的dt是从BLL获取的数据源
二级菜单的绑定:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
BLL.Permission_permitsBLL Permissioin_permitsBLL = new BLL.Permission_permitsBLL();
System.Data.DataTable dt = Permissioin_permitsBLL.GetAllPermission();
System.Data.DataView dv = dt.DefaultView;
HiddenField hidden = e.Item.FindControl("HiddenField1") as HiddenField;
dv.RowFilter = "belongTo=" + hidden.Value;
Repeater rep = e.Item.FindControl("Repeater3") as Repeater;
rep.DataSource = dv;
rep.DataBind();
}
}
三级菜单的绑定
protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
BLL.Permission_permitsBLL Permissioin_permitsBLL = new BLL.Permission_permitsBLL();
System.Data.DataTable dt = Permissioin_permitsBLL.GetAllPermission();
//用来显示字典表里的remarks
//因为要绑定的是name字段
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["name"] = dt.Rows[i]["name"].ToString() +"("+ dt.Rows[i]["remarks"].ToString()+")";
}
System.Data.DataView dv = dt.DefaultView;
HiddenField hidden = e.Item.FindControl("HiddenField2") as HiddenField;
dv.RowFilter = "belongTo=" + hidden.Value;
CheckBoxList cbl = e.Item.FindControl("cblMember") as CheckBoxList;
cbl.DataTextField = "name";
cbl.DataValueField = "id";
cbl.DataSource = dv;
cbl.DataBind();
}
}
界面显示如下:
权限分配
接下来就是具体的权限分配了,也是比较难控制的地方
for (int i = 0; i < Repeater1.Items.Count; i++)
{
Repeater rep = Repeater1.Items[i].FindControl("Repeater3") as Repeater;
HiddenField hidden = Repeater1.Items[i].FindControl("HiddenField1") as HiddenField;
//定义一个临时变量,只有勾选第三级,才包含前两级 防止父级菜单重复绑定
int fatherAndGrandFather = 0;
for (int j = 0; j < rep.Items.Count; j++)
{
HiddenField hidden2 = rep.Items[j].FindControl("HiddenField2") as HiddenField;
CheckBoxList cbl = rep.Items[j].FindControl("cblMember") as CheckBoxList;
//临时变量 防止父级菜单重复绑定
int tempFather = 0;
if (cbl.Items.Count > 0)
{
//这是在绑定三级
for (int k = 0; k < cbl.Items.Count; k++)
{
if (cbl.Items[k].Selected == true)
{
tempFather++;
if (tempFather == 1)
{
permissionId = permissionId + hidden.Value + "," + hidden2.Value + ",";
}
permissionId = permissionId + cbl.Items[k].Value + ",";
}
}
}
else
{
CheckBox cb = rep.Items[j].FindControl("CheckBox1") as CheckBox;
HiddenField h = rep.Items[j].FindControl("HiddenField2") as HiddenField;
if (cb.Checked == true)
{
fatherAndGrandFather++;
if (fatherAndGrandFather == 1)
{
permissionId = permissionId + hidden.Value + ",";
}
permissionId = permissionId + h.Value + ",";
}
}
}
}
通过上面的代码就可以拼接出一个权限字典的字符串,然后插入数据库之后,就可以通过treeview进行绑定了。
欢迎大家提出自己的宝贵意见!