我做过一个类似的,在查询时,遍历一你从数据库中得到的数据,然后设置chekbox为选中没有就行了,
更改就比较烦了,你得先在chekbox上做一个标记,标记在修改之前,有没有这个权限,在保存的时候,先看这个标记,如果是有,功能模块为选中,就是修改记录,如果标记没有,功能模块为选中为添加,如果标记没有,功能模块没有选中为删除,大概思路就之样了
更改就比较烦了,你得先在chekbox上做一个标记,标记在修改之前,有没有这个权限,在保存的时候,先看这个标记,如果是有,功能模块为选中,就是修改记录,如果标记没有,功能模块为选中为添加,如果标记没有,功能模块没有选中为删除,大概思路就之样了
能不能说的再详细一点呀,我还是一头雾水。最好能给个例子。如果可行,给你300分也行。
就像你说的保存时很好做,但修改时有点困难;我的权限列表是用的Datagrid加入了模板列一列Checkbox,和三列RadioButton。Datagrid中有一列是主键,保存的时候将选中的这一行的主键存到数据库中,修改时根据这个主键所在的行来选中Checkbox的,Radiobutton也是一样的。
在我的实现方式下,我连保存都做不到,我要疯了。谁能帮帮我呢?问题解决了我给300分
令开铁子。
function SelectInput(n)
{
document.all("add"+n).select = true;
document.all("del"+n).select = true;//如果你也想默认选择这两个的话
document.all("modify"+n).select = true;
}提交的时候,Request["add1"] Request["del1"] Request["modify1"]如果null或者""则加个空的标志组合个字符串,比如 "增加|空|空" 就是代表能增加不能修改和删除。然后insert到数据库中。信息为用户 模块 权限
张三 模块ID 增加|空|空
PermissionService:应用级的变量,为其它页面提供权限验证服务。
public class PermissionService
{
private DataSet ds; public static PermissionService Current
{
get
{
if(System.Web.HttpContext.Current==null)
{
throw new Exception("Not found Http Context.");
} PermissionService service=null;
if(System.Web.HttpContext.Current.Application["PermissionService"]==null)
{
service=new PermissionService();
System.Web.HttpContext.Current.Application["PermissionService"]=service;
}
else
{
service=System.Web.HttpContext.Current.Application["PermissionService"] as PermissionService;
}
System.Web.HttpContext.Current.Application.Lock();
return service;
}
} public static void ReleaseCurrentService()
{
System.Web.HttpContext.Current.Application.UnLock();
} public bool Authenticate(string userId,string target,string operate,string metadata)
{
string sFormat="UserId='"+userId+"' And Target='{0}' And Operate='{1}'",sFormat2="UserId='"+userId+"' And Target='{0}' And Operate='{1}' And MetaData='{2}'"; switch(target)
{
case "Route":
//operate="Add";
target="Node";
break;
case "Loop":
//operate="Add";
target="Node";
break;
case "ComponentUsing":
target="Node";
break;
case "Support":
if(metadata != null)
{
target="Node";
}
break;
case "Project":
target="Node";
break;
case "Node":
break;
default:
metadata=null;
break;
} if(metadata != null)
{
return this.ds.Tables[0].Select(string.Format(sFormat2,target,operate,metadata)).Length>0;
}
else
{
return this.ds.Tables[0].Select(string.Format(sFormat,target,operate)).Length>0;
}
} protected PermissionService()
{
Init();
}
protected void Init()
{
using(SqlDAOHelper helper=new SqlDAOHelper())
{
this.ds=helper.ExecuteQueryByTextAsDataSet("Select UserId,Target,Operate,MetaData From Permission");
}
}
}
}
你的代码我也看不太懂,不过好像和我所要实现的不太一样吧。
我现在的难点是:选了左边的模块后,在选右边的操作功能。但我怎么知道,右边的操作功能
属于右边哪个功能模块的呢?
我水平有限,谁有完整的例子呢?帮帮我吧。
{
get
{
return this.ViewState["DataSource"] as DataSet;
}
set
{
this.ViewState["DataSource"]=value;
}
} private void Page_Load(object sender, System.EventArgs e)
{
if(this.Session["UserId"].ToString() !="administrator")
{
this.Response.Redirect("AccessDeny.htm");
} if(!this.IsPostBack)
{
LoadUserList();
LoadData();
ParseData(this.ddlUserList.SelectedItem.Value);
} } private void LoadUserList()
{
DataSet ds; string sql="Select UserId,ManName From UserManView";
using(SqlDAOHelper helper=new SqlDAOHelper())
{
ds=helper.ExecuteQueryByTextAsDataSet(sql);
} if(ds==null) return; this.ddlUserList.DataSource=ds.Tables[0];
this.ddlUserList.DataValueField="UserId";
this.ddlUserList.DataTextField="ManName";
this.ddlUserList.DataBind();
} private void LoadData()
{
this.ds=new DataSet();
this.sqlConnection1.Open();
this.sqlDataAdapter1.Fill(this.ds);
this.sqlConnection1.Close();
} private void ParseData(string userId)
{
int i;
CheckBoxList cbl;
string[] rawData;
string sql;
string sFormat="UserId='"+userId+"' And Target='{0}' And Operate='{1}'",sFormat2="UserId='"+userId+"' And Target='{0}' And Operate='{1}' And MetaData='{2}'"; for(i=200;i<=207;i++)
{
cbl=this.FindControl("cbl"+i.ToString()) as CheckBoxList;
foreach(ListItem item in cbl.Items)
{
rawData=item.Value.Split(new char[]{'#'});
sql=string.Format(sFormat,rawData); if(this.ds.Tables[0].Select(sql).Length>0)
{
item.Selected=true;
}
else
{
item.Selected=false;
}
} } for(i=300;i<=313;i++)
{
cbl=this.FindControl("cbl"+i.ToString()) as CheckBoxList;
foreach(ListItem item in cbl.Items)
{
rawData=item.Value.Split(new char[]{'#'});
sql=string.Format(sFormat2,rawData); if(this.ds.Tables[0].Select(sql).Length>0)
{
item.Selected=true;
}
else
{
item.Selected=false;
}
} }
} private void Update()
{
string userId=this.ddlUserList.SelectedItem.Value;
int i;
CheckBoxList cbl;
string[] rawData;
string sql;
DataRow row;
string sFormat="UserId='"+userId+"' And Target='{0}' And Operate='{1}'",sFormat2="UserId='"+userId+"' And Target='{0}' And Operate='{1}' And MetaData='{2}'"; for(i=200;i<=207;i++)
{
cbl=this.FindControl("cbl"+i.ToString()) as CheckBoxList;
foreach(ListItem item in cbl.Items)
{
rawData=item.Value.Split(new char[]{'#'});
sql=string.Format(sFormat,rawData); if(item.Selected)
{
if(this.ds.Tables[0].Select(sql).Length>0)
{
continue;
}
else
{
row=this.ds.Tables[0].NewRow();
row["UserId"]=userId;
row["Target"]=rawData[0];
row["Operate"]=rawData[1];
this.ds.Tables[0].Rows.Add(row);
}
}
else
{
if(this.ds.Tables[0].Select(sql).Length>0)
{
this.ds.Tables[0].Select(sql)[0].Delete();
}
else
{
continue;
}
}
} } for(i=300;i<=313;i++)
{
cbl=this.FindControl("cbl"+i.ToString()) as CheckBoxList;
foreach(ListItem item in cbl.Items)
{
rawData=item.Value.Split(new char[]{'#'});
sql=string.Format(sFormat2,rawData); if(item.Selected)
{
if(this.ds.Tables[0].Select(sql).Length>0)
{
continue;
}
else
{
row=this.ds.Tables[0].NewRow();
row["UserId"]=userId;
row["Target"]=rawData[0];
row["Operate"]=rawData[1];
row["MetaData"]=rawData[2];
this.ds.Tables[0].Rows.Add(row);
}
}
else
{
if(this.ds.Tables[0].Select(sql).Length>0)
{
this.ds.Tables[0].Select(sql)[0].Delete();
}
else
{
continue;
}
}
} }
} private void Apply()
{
this.sqlConnection1.Open();
int result=this.sqlDataAdapter1.Update(this.ds.Tables[0]);
this.sqlConnection1.Close(); if(result>0)
{
PermissionService.Current.Update();
this.RegisterStartupScript("closeWindow()",@"<script language=javascript>window.alert('功能完成设置,窗口将关闭.');window.close();</script>");
}
else
{
ClientAction.DoAlert(false);
}
}
private void ddlUserList_SelectedIndexChanged(object sender, System.EventArgs e)
{
ParseData(this.ddlUserList.SelectedItem.Value);
} private void bnApplyLastUser_Click(object sender, System.EventArgs e)
{
ParseData(this.ViewState["LastUser"].ToString());
} private void bnOkay_Click(object sender, System.EventArgs e)
{
this.Update();
this.ViewState["LastUser"]=this.ddlUserList.SelectedItem.Value;
this.bnApplyLastUser.Enabled=true; } private void bnApply_Click(object sender, System.EventArgs e)
{
this.Update();
this.Apply();
}
我的和你的不太一样呀,我把我的代码给你发过去,能帮我解决一下吗?
能告送我你的邮箱吗
PermissionId UserId Operate Target MetaData
1 wzd View Node <Null>
2 wzd Add Document 云南&丽江这里的MetaData字段是用来存储太详细的权限细分的情况。一条记录就给用户分配了一个权限。
PermissionService是为了避免频繁查数据库,我们把Permission表中数据Cache起来。在Permission表中,我们把所有的数据先读到DataSet中。
然后所有的单条记录我们就存在一个.checkboxlist,哪为什么不用checkbox呢,因为checkboxlist的listitem有三个属性:text,value,selected。我们可以通过value来存储一条权限的信息,用selected来判断用户是否用这个权限。这样,我们就可以通过dataset来加载所有checkboxlist.而用户改变权限后,我们又根据checkboxlist来更新dataset。最后通过adapter把改变写回数据库。