📄 admin.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xmlOp;
public partial class setAdmin : System.Web.UI.Page
{
string xmlFile= ConfigurationManager.AppSettings["xmlFile"];
Logic lg = new Logic(); //创建Logic类对象
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//验证是否管理员
if (Convert.ToString(Session["NormalAdmin"]) != "yes")
{
Response.Write("你没有权限查看!");
Response.End();
}
BindToDataGrid(); //数据绑定到DataGrid控件
}
}
//数据绑定到DataGrid,显示班级所有成员
private void BindToDataGrid()
{
try
{
XmlOp op = new XmlOp(xmlFile);
ds = op.GetDs("//Root");
this.DataGrid1.DataKeyField = "Name";
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
}
catch
{
Response.Write("出现不明错误 !");
Response.End();
}
}
//每项数据绑定时事件
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick", "return confirm('你确定要删除吗?');");
if (Convert.ToString(Session["SuperAdmin"]) == "yes") //若为超级管理员
{
((Button)(e.Item.Cells[7].FindControl("btnSetAdmin"))).Visible = true; //显示设置管理员按钮
((Button)(e.Item.Cells[7].FindControl("btnNoAdmin"))).Visible = true; //显示取消管理员按钮
}
}
}
//删除某个同学
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
XmlOp op = new XmlOp(xmlFile);
string _delName = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); //取得关键字段
string _delNode = "//Root/Student[Name='" + _delName + "']";
string _photo = op.SelectNodeText("//Root/Student[Name='" + _delName + "']/Photo");
if (op.DeleteNode(_delNode)) //删除操作
{
op.Save(xmlFile); //保存Xml文档
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(_photo)))
{
try
{
//删除相应图片
System.IO.File.Delete(HttpContext.Current.Server.MapPath(_photo));
}
catch (Exception ex)
{
throw ex;
}
}
Response.AddHeader("refresh", "0"); //刷新整个页面
}
}
//一般管理员的设置
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
XmlOp op = new XmlOp(xmlFile);
//设置一般管理员
if (e.CommandName == "SetAdmin")
{
string _adminName = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
if (op.UpdateAttrib("//Root/Student[Name='" + _adminName + "']", "Admin", "yes"))
{
op.Save(xmlFile);
lg.Msg("设置成功!");
BindToDataGrid();
}
}
//取消一般管理员
if (e.CommandName == "NoAdmin")
{
string _adminName = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
if (op.UpdateAttrib("//Root/Student[Name='" + _adminName + "']", "Admin", "no"))
{
op.Save(xmlFile);
lg.Msg("设置成功!");
BindToDataGrid();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -