📄 admin_manageuser.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.Text;
namespace dayi
{
/// <summary>
/// admin_manageUser 的摘要说明。
/// </summary>
public class admin_manageUser : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblUserList;
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Button btnUnlock;
protected System.Web.UI.WebControls.Button btnLock;
dayi.controlDatabase cd=new controlDatabase();
private string showUserList(string type,int pageCount,int curPage)
{
//显示普通用户列表
//type:显示类别,lock为锁定,normal为正常
//pageCount:每页多少条记录
//curPage:当前页
//返回用户列表
//-------------------------
//初始化参数
curPage=1;
if(Request.QueryString["page"]!=null)
curPage=Convert.ToInt32(Request.QueryString["page"]);
if(Request.QueryString["type"]!=null)
type=Request.QueryString["type"].ToString();
string viewUserPage="<a href=admin_showUserDetail.aspx?id=";
if(pageCount<=0)
pageCount=20;
//--------------------------
StringBuilder returnValue=new StringBuilder();
string sql="select * from [user] order by locked desc";
if(type=="lock")
{
sql="select * from [user] where locked='1' order by userName";
}
if(type=="normal")
{
sql="select * from [user] where locked='0' order by userName";
}
int totalRecord=0;//总记录数
int totalPage=0;//总页数
try
{
DataTable dt=cd.getDataTable(sql);
//得到总记录数
totalRecord=dt.Rows.Count;
//得到总页数
totalPage=(totalRecord+pageCount-1)/pageCount;
if(curPage<=0)
curPage=1;
else if(curPage>totalPage)
curPage=totalPage;
if(totalPage>0)
{
returnValue.Append("<table width=\"600\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" bgcolor=\"#003366\"><tr bgcolor=\"#0066CC\">");
returnValue.Append("<td><div align=\"center\" class=\"style1\"> </div></td>");
returnValue.Append("<td><div align=\"center\" class=\"style1\">用户名</div></td>");
returnValue.Append("<td><div align=\"center\" class=\"style1\">Email</div></td>");
returnValue.Append("<td><div align=\"center\" class=\"style1\">注册时间</div></td>");
returnValue.Append("<td><div align=\"center\" class=\"style1\">问题数</div></td>");
returnValue.Append("<td><div align=\"center\" class=\"style1\">操作</div></td></tr>");
//实现分页
int startLine=(curPage-1)*pageCount;
DataRow row;
for(int i=startLine;i<(pageCount+startLine);i++)
{
if(i<totalRecord)
{
row=dt.Rows[i];
//得到用户所发问题的数量
DataTable count=cd.getDataTable("select * from [questions] where askuser='"+row["userName"].ToString().Trim()+"'");
string questionCount=count.Rows.Count.ToString();
string manage="<a href=admin_lockOrDeleteUser.aspx?type=lock&id="+row["userID"].ToString()+">锁定</a>|";
if(row["locked"].ToString()=="1")
{
manage="<a href=admin_lockOrDeleteUser.aspx?type=unlock&id="+row["userID"].ToString()+">解锁</a>|";
}
manage+="<a href=admin_lockOrDeleteUser.aspx?type=delete&id="+row["userID"].ToString()+">删除</a>";
returnValue.Append("<tr bgcolor=\"#FFFFFF\"><td><div align=\"center\"><INPUT type=\"checkbox\" name=\"chkDelete\" value=\""+row["userID"].ToString()+"\"></div></td>");
returnValue.Append("<td><div align=\"left\">"+viewUserPage+row["userID"].ToString()+">"+row["userName"].ToString()+"</a></div></td>");
returnValue.Append("<td><div align=\"center\"><a href=\"mailto:"+row["userEmail"].ToString().Trim()+"\">"+row["userEmail"].ToString().Trim()+"</a></div></td>");
returnValue.Append("<td><div align=\"center\">"+row["regTime"].ToString().Trim()+"</div></td>");
returnValue.Append("<td><div align=\"center\">"+questionCount+"</div></td>");
returnValue.Append("<td><div align=\"center\">"+manage+"</div></td></tr>");
}
else
{
break;
}
}
returnValue.Append("</table>");
if(totalPage>1)
{
returnValue.Append(cd.showPageString("<a href=admin_manageUser.aspx?type="+type,curPage,totalPage,pageCount,totalRecord));
}
return returnValue.ToString();
}
else
{
//返回空
return "";
}
}
catch(Exception ee)
{
//throw new Exception();
Response.Write(ee.ToString());
return "";
}
}
private void check()
{
if(this.lblUserList.Text.Trim()=="")
{
this.btnDelete.Visible=false;
this.btnUnlock.Visible=false;
this.btnLock.Visible=false;
this.lblUserList.Text="没有任何记录";
}
else
{
this.btnDelete.Visible=true;
this.btnUnlock.Visible=true;
this.btnLock.Visible=true;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
if(Session["userType"].ToString()!="admin")
{
//确认身份
Response.Redirect("err.aspx?err=没有权限");
}
try
{
this.lblUserList.Text=this.showUserList("",20,1);
this.check();
}
catch(Exception ee)
{
Response.Write(ee.ToString());
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.btnLock.Click += new System.EventHandler(this.lblLock_Click);
this.btnUnlock.Click += new System.EventHandler(this.btnUnlock_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnDelete_Click(object sender, System.EventArgs e)
{
//删除选中用户
try
{
if(Request.Form["chkDelete"]!=null)
{
string idList=Request.Form["chkDelete"].ToString();
string sql="";
if(idList.Trim()!="")
{
sql="delete from [user] where userID in("+idList+")";
cd.updateDatabase(sql);
this.lblUserList.Text=this.showUserList("",20,1);
this.check();
}
}
}
catch(Exception ee)
{
Response.Write(ee.ToString());
}
}
private void lblLock_Click(object sender, System.EventArgs e)
{
//锁定选中用户
try
{
if(Request.Form["chkDelete"]!=null)
{
string idList=Request.Form["chkDelete"].ToString();
string sql="";
if(idList.Trim()!="")
{
sql="update [user] set locked='1' where userID in("+idList+")";
cd.updateDatabase(sql);
this.lblUserList.Text=this.showUserList("",20,1);
this.check();
}
}
}
catch(Exception ee)
{
Response.Write(ee.ToString());
}
}
private void btnUnlock_Click(object sender, System.EventArgs e)
{
//解除锁定选中用户
try
{
if(Request.Form["chkDelete"]!=null)
{
string idList=Request.Form["chkDelete"].ToString();
string sql="";
if(idList.Trim()!="")
{
sql="update [user] set locked='0' where userID in("+idList+")";
cd.updateDatabase(sql);
this.lblUserList.Text=this.showUserList("",20,1);
this.check();
}
}
}
catch(Exception ee)
{
Response.Write(ee.ToString());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -