这是这个权限控制的第一步,扫描界面把要分配权限的元素的信息获取出来存入到数据库中。
这一步分三小步:
(1).标出界面所要分配权限的元素
(2).扫描界面获取所要分配权限的元素信息。(id,标题,层级关系)
(3).存入数据库中。
1.标出界面所要分配权限的元素.
在扫描的时候一开始我觉得很难因为html元素过多又有很多层级关系。一开始用的是dc6dce4a544fdca2df29d5ac0ea9906b标签来表示html所要分配权限的元素,发现这个方案不行,比如把添加用户按钮加上div那么这个按钮的样式就变了还得调样式我现在做的ks系统有将近100个界面,再加上现在的界面已经确定好样式,调好css,加上再加上div的话那么得重新调界面,就这个问题想了两三天,最后我想到要不自定义一个html元素标签为:a8a2d94ad6460784ca7a88c66ac1760ac9d65ada34ddc33d0c7adaabb88cddc9为这个标签加上id和title为:a65074dfa9178d5ee5accdb784727c43c9d65ada34ddc33d0c7adaabb88cddc9,用这个标签来标记所要分配权限的元素。这样的话既不用更改样式又能获取到所要的信息。更改之后的界面后台代码为:
<!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> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<meta name="author" content="kudychen@gmail.com" /> 
<meta http-equiv="x-ua-compatible" content="ie=emulateie7" /> 
<title>用户管理——查询用户</title> 
<script src="../../js/div/jquery.js"></script> 
<script src="../../js/div/div.js"></script> 
<link href="../../css/admin.global.css" rel="stylesheet" type="text/css" /> 
<link href="../../css/admin.content.css" rel="stylesheet" type="text/css" /> 
<style type="text/css"> 
.btn-middle { 
width: 76px; 
} 
</style> 
</head> 
<body> 
<form id="form" method="post" runat="server" > 
<%--box标记 main 开始标记--%> 
<box id="main" title="主要"> 
<div class="location">当前位置:用户管理 -> 查询用户</div> 
<div class="blank10"></div> 
<%--box标记 queryuser 开始标记--%> 
<box id="queryuser" title="查询用户"> 
<div class="search block" > 
<div class="h"> 
<span class="icon-sprite icon-magnifier"></span> 
<h3>快速搜索</h3> 
</div> 
<div class="tl corner"></div> 
<div class="tr corner"></div> 
<div class="bl corner"></div> 
<div class="br corner"></div> 
<div class="cnt-wp"> 
<div class="cnt"> 
<div class="search-bar"> 
<label class="first txt-green">用户名:</label> 
<input value="" type="text" name="username" id="username" class="input-small" /> 
<asp:linkbutton id="lbtquery" class="btn-lit" runat="server" 
onclick="lbtquery_click" width="58px"><span >查询</span></asp:linkbutton> 
<%--box标记 adduser 开始标记--%> 
<box id="adduser" title="添加用户"> 
<a class="btn-lit btn-middle" href="admadduser.aspx"> 
<span>添加用户</span> 
</a> 
</box> 
<%--box标记 adduser结束标记--%> 
</div> 
</div> 
</div> 
</div> 
</box> 
<%--box标记 queryuser结束标记--%> 
<%--box标记 userlist开始标记--%> 
<box id="userlist" title="用户列表"> 
<span class="block"> 
<div class="tl corner"></div> 
<div class="tr corner"></div> 
<div class="bl corner"></div> 
<div class="br corner"></div> 
<div class="cnt-wp"> 
<div class="cnt"> 
<div class="h"> 
<span class="icon-sprite icon-list"></span> 
<h3>用户列表</h3> 
</div> 
<div class="blank10"></div> 
<asp:repeater id="rpuserinfo" runat="server" onitemcommand="rpuserinfo_itemcommand"> 
<headertemplate> 
<table class="data-table history" id="maintable" border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<th scope="col">用户名称</th> 
<th scope="col">角色</th> 
<th scope="col">操作记录</th> 
<th scope="col">编辑</th> 
</tr> 
</headertemplate> 
<itemtemplate> 
<tr> 
<td class="txt160 c"><%#eval("username")%></td> 
<td class="txt c"><%#eval("rolename")%></td> 
<td class="txt80 c"><a href="admoperatorrecord.aspx?userid=<%#eval("userno") %>" title="操作记录">操作记录</a></td> 
<td class="icon"> 
<%--box标记 edituserinfo开始标记--%> 
<box id="edituserinfo" title="编辑用户"> 
<a class="opt" title="编辑" href="admupdateuser.aspx?userid=<%#eval("userno") %>"> 
<span class="icon-sprite icon-edit"> 
</span> 
</a> 
</box> 
<%--box标记 edituserinfo结束标记--%> 
</td> 
</tr> 
</itemtemplate> 
<footertemplate> 
</table> 
</footertemplate> 
</asp:repeater> 
</div> 
</div> 
</span> 
<span id="spanfirst">第一页</span> <span id="spanpre">上一页</span> <span id="spannext">下一页</span> <span id="spanlast">最后一页</span> 第<span id="spanpagenum"></span>页/共<span id="spantotalpage"></span>页 
</box> 
<%--box标记 userlist 结束标记--%> 
</box> 
<%--box标记 main 结束标记--%> 
</form> 
</body> 
</html>
2.扫描界面获取所要分配权限的元素信息。
由于界面上有了<box>这个元素来表示权限的元素,这样扫描起来就好一些了不过还是遇到了不少的问题,最终还是解决了,最难的是扫描出两个<box>之间的父子关系。下面是js代码:
$(document).ready(function () { 
var rootboxs = document.getelementbyid("main"); 
var child = rootboxs.childnodes; 
findchildbox(child) 
}); 
//搜寻子节点 
function findchildbox(parentnode) { 
for (var i = 0; i < parentnode.length; i++) { 
/// 
if (parentnode[i].nodename == "box") { 
var childboxid = parentnode[i].id; 
var childboxtitle = encodeuri(parentnode[i].title); 
var parentbox = findparentbox(parentnode[i].parentnode); 
var parentboxid = parentbox.id; 
if (window.xmlhttprequest) { 
//ie7 above,firefox,chrome^^ 
xmlhttp = new xmlhttprequest(); 
//为了兼容部分mozillar浏览器,当来自服务器响应开头不是xml,导致的无法响应问题 
if (xmlhttp.overridemimetype) { 
xmlhttp.overridemimetype('text/xml'); 
} 
} 
else if (window.activexobject) { 
//ie5\ie6 
xmlhttp = new activexobject("microsoft.xmlhttp"); 
} 
if (xmlhttp == null || xmlhttp == undefined) { 
alert("con't create xmlhttprequest object"); 
} 
//注册回调函数 
xmlhttp.onreadystatechange = callback; 
//发送信息 
xmlhttp.open('get', '../../manager/rolemanager/addbox.ashx?childboxid=' + childboxid + '&childboxtitle=' + childboxtitle + '&parentboxid=' + parentboxid, true); 
xmlhttp.send(null); 
function callback() { 
//判断交互是否完成,是否正确返回 
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { 
} 
} 
} 
findchildbox(parentnode[i].childnodes) 
} 
} 
//查询父节点 
function findparentbox(child) { 
if (child.nodename == "box") { 
return child; 
} else { 
return findparentbox(child.parentnode) 
} 
}
3.存入数据库中。
利用ajax存入到数据库中,一开始就遇到了问题,因为扫描界面所需要的时间太短在还没有把第一条数据插入到数据库的时候第二条数据就来了这样导致了第一条数据的部分信息就会被第二条记录替代了导致存入数据库的数据出现了问题。一开始我是打算在js那里加上个延迟,结果表名不行。然后我就在一班程序里面加入一个类似锁的一个东西,算作延迟吧这样存入的数据就不会错误了下面是代码:
using system; 
using system.collections.generic; 
using system.linq; 
using system.web; 
using bll.manager.roleusermanagerbll; 
using system.data; 
using system.text; 
using model; 
using bll; 
namespace examsystemv3.manager.rolemanager 
{ 
/// <summary> 
/// addbox 的摘要说明 
/// </summary> 
public class addbox : ihttphandler 
{ 
public void processrequest(httpcontext context) 
{ 
context.response.contenttype = "text/plain"; 
system.threading.thread.sleep(1000); 
diventity ediv = new diventity(); 
admdivmanager admdivmanager = new admdivmanager(); 
publicbll publicbll = new publicbll(); 
string strchildboxid = ""; 
string strchildboxtitle = ""; 
strchildboxid = context.request.querystring["childboxid"].tostring().trim(); 
strchildboxtitle = context.server.urldecode(context.request.querystring["childboxtitle"].tostring().trim()); 
string strparentboxid=context.request.querystring["parentboxid"].tostring ().trim();; 
string strstate = "是"; 
string strdatetime = publicbll.getdate(); 
string strip = publicbll.getwebclientip(); 
string stroperator ="xvshu";//context.session["userno"].tostring().trim(); ; 
ediv.id = strchildboxid; 
ediv.mainrelation = strparentboxid; 
ediv.divname = strchildboxtitle; 
ediv.divdescribe = strchildboxtitle; 
ediv.operator = stroperator; 
ediv.operatorip = strip; 
ediv.state = strstate; 
ediv.datetime = strdatetime; 
admdivmanager.adddiv(ediv); 
} 
public bool isreusable 
{ 
get 
{ 
return false; 
} 
} 
} 
}
利用treeview控件显示出来如下图:
更多asp.net对html页面元素进行权限控制(二)。
   
 
   