📄 userroles.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using OI.DatabaseOper;
namespace OI.Manage
{
/// <summary>
/// UserRoles 的摘要说明。
/// </summary>
public class UserRoles : OI.PageBase
{
protected System.Web.UI.WebControls.CheckBoxList CheckBoxListPowers;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label LabelMsg;
protected System.Web.UI.WebControls.ListBox Lst_roles;
protected System.Web.UI.HtmlControls.HtmlForm userPowerManage;
protected System.Web.UI.WebControls.ImageButton ImageButtonSave;
protected System.Web.UI.WebControls.ImageButton ImageButtonCancel;
protected DatabaseConnect op=new DatabaseConnect();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (Session["userid"]==null)
{
Response.Write ("<script>alert('超时,请重新登录');top.location.href='../userpass.aspx';</script>");
return ;
}
if(!Page.IsPostBack)
{
//BindListBox();//绑定所有可用的用户
DisplayRoles();
BindCheckBoxList();//取得所有可用的权限
}
}
/// <summary>
/// 绑定所有可用的用户
/// </summary>
private void DisplayRoles()
{
string strsql ="select UserID,username from accounts_users order by username ";
DataSet ds=new DataSet ();
ds=op.getBinding (strsql,"t");
Lst_roles.DataSource =ds.Tables[0];
Lst_roles.DataTextField =ds.Tables[0].Columns["username"].ToString ();
Lst_roles.DataValueField =ds.Tables[0].Columns["UserID"].ToString ();
Lst_roles.DataBind ();
}
/// <summary>
/// 绑定选定的用户所拥有的权限
/// </summary>
/// <param name="userid"></param>
private void BindCheckBoxList(string userid)
{
string sql="select * from Accounts_UserRoles where UserID="+userid;
// if (int.Parse (op.GetValueBySql (sql)) >0)
if(op.getData(sql).Count>0) //有用户有权限的情况
{
BindCheckBoxList();
string osql="select roleid from Accounts_UserRoles where userid="+userid;
System.Collections.ArrayList al=op.getData(osql);
for(int i=0;i<CheckBoxListPowers.Items .Count ;i++)
{
for(int j=0;j<al.Count ;j++)
{
if(CheckBoxListPowers.Items [i].Value .Trim ().Equals (al[j].ToString ().Trim ()))
CheckBoxListPowers.Items [i].Selected =true;
}
}
}
else //用户没有设定权限的情况
{
for(int i=0;i<CheckBoxListPowers.Items.Count;i++)
{
CheckBoxListPowers.Items [i].Selected =false;
}
}
}
/// <summary>
/// 取得所有可用的权限
/// </summary>
private void BindCheckBoxList()
{
string resql="select roleid,rolename from accounts_roles order by rolename";
CheckBoxListPowers.DataSource=op.getBinding(resql,"Powers");
CheckBoxListPowers.DataTextField="rolename";
CheckBoxListPowers.DataValueField="roleid";
CheckBoxListPowers.DataBind();
for(int i=0;i<CheckBoxListPowers.Items.Count;i++)
{
CheckBoxListPowers.Items[i].Selected=false;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Lst_roles.SelectedIndexChanged += new System.EventHandler(this.Lst_roles_SelectedIndexChanged);
this.ImageButtonSave.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButtonSave_Click);
this.ImageButtonCancel.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButtonCancel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Lst_roles_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindCheckBoxList(Lst_roles.SelectedItem.Value);
LabelMsg.Text="";
}
private void ImageButtonCancel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
BindCheckBoxList(Lst_roles.SelectedItem.Value);
}
private void ImageButtonSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Lst_roles.SelectedIndex>-1)
{
System.Collections.ArrayList al=new System.Collections.ArrayList();
string sql1="delete from Accounts_UserRoles where userid='"+Lst_roles.SelectedItem.Value+"'";
al.Add((object)sql1);//将删除语句加入事务以备处理
string sql="";
for(int i=1;i<CheckBoxListPowers.Items.Count+1;i++)
{
if(CheckBoxListPowers.Items[i-1].Selected)
{ //如果选定就记录他的Id号
sql="insert into Accounts_UserRoles values('"+Lst_roles.SelectedItem.Value +"','"+ CheckBoxListPowers.Items[i-1].Value+"')";//写入数据库
al.Add((object)sql);
}
}
if(op.ExecuteTransaction(al))
{
LabelMsg.Text="<font color=Red>权限处理成功</font>";
}
}
else
{
Page.RegisterStartupScript ("","<script>alert('请选择要设定权限的用户')</script>");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -