📄 permissionsetting.ascx.cs
字号:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class controls_Permission_PermissionSetting : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRoles();
BindUsers();
}
/******同步viewstate(userrole)的值和griduser的值*********/
for (int i = 0; i < grdUsers.Rows.Count; i++)
{
UserRole.Rows[i][1] =
((CheckBox)grdUsers.Rows[i].FindControl("chkView")).Checked;
UserRole.Rows[i][2] =
((CheckBox)grdUsers.Rows[i].FindControl("chkEdit")).Checked;
}
/*********同步viewstate["rolepermission"]和gridroles的值*******/
for (int i = 0; i < grdRoles.Rows.Count; i++)
{
DataRow[] ro = RolePermission.Select("RoleName='" + grdRoles.Rows[i].Cells[0].Text.Replace("'", "''") + "'");
if (ro.Length == 0)
{
if (((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Checked ||
((CheckBox)grdRoles.Rows[i].FindControl("chkEdit")).Checked)
{
RolePermission.Rows.Add(
new object[]
{
grdRoles.Rows[i].Cells[0].Text,
((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Checked,
((CheckBox)grdRoles.Rows[i].FindControl("chkEdit")).Checked
}
);
}
}
else
{
for (int j = 0; j < ro.Length; j++)
{
if (!ro[j][1].Equals(((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Checked))
ro[j][1] = ((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Checked;
if (!ro[j][2].Equals(((CheckBox)grdRoles.Rows[i].FindControl("chkEdit")).Checked))
ro[j][2] = ((CheckBox)grdRoles.Rows[i].FindControl("chkEdit")).Checked;
}
}
}
/*********去掉viewstate["rolepermission"]中administrators组的设置和都没有勾的设置**********/
DataRow[] delro = RolePermission.Select("(View=false and Edit=false) or RoleName='Administrators'");
for (int i = 0; i < delro.Length; i++)
{
RolePermission.Rows.Remove(delro[i]);
}
}
private void BindRoles()
{
string[] roles = Roles.GetAllRoles();
DataTable r = new DataTable();
r.Columns.Add("RoleName");
r.Columns.Add("View", typeof(bool));
r.Columns.Add("Edit", typeof(bool));
r.Rows.Add("Administrators", true, true);
r.Rows.Add("Anonymous", false, false);
r.Rows.Add("Registered Users", false, false);
for (int i = 0; i < roles.Length; i++)
{
if (roles[i] != "Administrators" && roles[i] != "Anonymous" && roles[i] != "Registered Users")
{
r.Rows.Add(new object[] { roles[i], false, false });
}
}
/****************/
for (int i = 0; i < r.Rows.Count; i++)
{
DataRow[] ro = RolePermission.Select("RoleName='" + r.Rows[i][0].ToString().Replace("'", "''") + "'");
if (ro.Length > 0)
{
r.Rows[i][1] = ro[0][1];
r.Rows[i][2] = ro[0][2];
}
}
grdRoles.DataSource = r;
grdRoles.DataKeyNames = new string[] { "RoleName" };
grdRoles.DataBind();
}
private void BindUsers()
{
grdUsers.DataSource = UserRole;
grdUsers.DataKeyNames = new string[] { "UserName" };
grdUsers.DataBind();
}
/// <summary>
/// 用户组权限表
/// </summary>
public DataTable RolePermission
{
get
{
if (ViewState["rolepermission"] == null)
{
DataTable r = new DataTable();
r.Columns.Add("RoleName");
r.Columns.Add("View", typeof(bool));
r.Columns.Add("Edit", typeof(bool));
ViewState["rolepermission"] = r;
}
return (DataTable)ViewState["rolepermission"];
}
set
{
ViewState["rolepermission"] = value;
}
}
/// <summary>
/// 用户权限表
/// </summary>
public DataTable UserRole
{
get
{
if (ViewState["userrole"] == null)
{
DataTable r = new DataTable();
r.Columns.Add("UserName");
r.Columns.Add("View", typeof(bool));
r.Columns.Add("Edit", typeof(bool));
ViewState["userrole"] = r;
}
return (DataTable)ViewState["userrole"];
}
set
{
ViewState["userrole"] = value;
}
}
/// <summary>
/// 是否可以设置浏览权限
/// </summary>
public bool SetView
{
get
{
if (ViewState["setview"] == null)
{
ViewState["setview"] = false;
}
return (bool)ViewState["setview"];
}
set
{
ViewState["setview"] = value;
}
}
/// <summary>
/// 新建用户的权限
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkBtnAdd_Click(object sender, EventArgs e)
{
if (Membership.GetUser(txtUserName.Text) == null)
{
lblerr.Text = "请输入一个有效的用户名";
return;
}
UserRole.Rows.Add(new object[] { txtUserName.Text, false, false });
BindUsers();
}
/// <summary>
/// 删除用户权限时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
UserRole.Rows.RemoveAt(e.RowIndex);
BindUsers();
}
/*显示时,根据是否可以设置浏览权限来修改checkbox的enable*/
protected void Page_PreRender(object sender, EventArgs e)
{
for (int i = 0; i < grdRoles.Rows.Count; i++)
{
/*如果是Administrators组则一定是有权限的*/
if (grdRoles.Rows[i].Cells[0].Text == "Administrators")
{
((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Enabled = false;
((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Checked = true;
((CheckBox)grdRoles.Rows[i].FindControl("chkEdit")).Enabled = false;
((CheckBox)grdRoles.Rows[i].FindControl("chkEdit")).Checked = true;
}
else
{
((CheckBox)grdRoles.Rows[i].FindControl("chkView")).Enabled = SetView;
}
}
for (int i = 0; i < grdUsers.Rows.Count; i++)
{
((CheckBox)grdUsers.Rows[i].FindControl("chkView")).Enabled = SetView;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -