📄 singleuserrole.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 OI.DatabaseOper ;
namespace OI.Manage
{
/// <summary>
/// singleUserRole 的摘要说明。
/// </summary>
public class singleUserRole : OI.PageBase
{
protected System.Web.UI.WebControls.CheckBoxList CheckBoxListPowers;
protected System.Web.UI.WebControls.ImageButton ImageButtonSave;
protected System.Web.UI.WebControls.ImageButton ImageButtonCancel;
protected System.Web.UI.WebControls.Label lblUserName;
protected System.Web.UI.WebControls.Label LabelMsg;
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 )
{
try
{
ViewState["userid"]=Request.Params ["userid"].ToString ();
object o=op.GetObjectBySql("select username from accounts_users where userid="+ ViewState["userid"].ToString ());
lblUserName.Text =o.ToString ();
}
catch
{
return;
}
BindCheckBoxList();
BindCheckBoxList(ViewState["userid"].ToString ());
}
}
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;
}
}
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>
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
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 ImageButtonSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
System.Collections.ArrayList al=new System.Collections.ArrayList();
string sql1="delete from Accounts_UserRoles where userid='"+ViewState["userid"].ToString ()+"'";
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('"+ViewState["userid"].ToString () +"','"+ CheckBoxListPowers.Items[i-1].Value+"')";//写入数据库
al.Add((object)sql);
}
}
if(op.ExecuteTransaction(al))
{
LabelMsg.Text="<font color=Red>权限处理成功</font>";
Page.RegisterStartupScript ("","<script>window.close()</script>");
}
}
private void ImageButtonCancel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
BindCheckBoxList(ViewState["userid"].ToString ());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -