📄 index.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 System.IO;
using xmlOp; //引入XmlOp命名空间
public partial class index : System.Web.UI.Page
{
string _xmlFile = ConfigurationManager.AppSettings["xmlFile"];
string _userName = ""; //记录用户名变量
string _userPwd = ""; //记录用户密码变量
Logic lg = new Logic(); //声明一个Logic类对象
DataSet ds = new DataSet(); //创建一个空DataSe对象
protected void Page_Load(object sender, EventArgs e)
{
//读取Cookie里的帐号和密码并赋值给变量
HttpCookie cookie = Request.Cookies["userInfo"];
if (cookie != null)
{
_userName = cookie.Values["userName"].ToString();
_userPwd = cookie.Values["userPwd"].ToString();
}
//页面第一次加载
if (!IsPostBack)
{
if (_userName == null || _userName=="") //非法访问时
{
Response.Write("你没有登录,<a href='default.aspx'>请登录</a> !");
Response.End();
}
BindToDataGrid(); //调用数据绑定函数
}
}
//数据绑定到DataGrid,显示班级所有成员
private void BindToDataGrid()
{
try
{
XmlOp op = new XmlOp(_xmlFile); //声明XmlOp类对象
ds = op.GetDs("//Root");
this.DataGrid1.DataKeyField = "Name"; //数据表关键字段集合
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
Session["thisDS"] = ds; //把数据集保存到Session变量中
BindToRepeater(this.Repeater1,1); //绑定Repeater1
}
catch
{
Response.Write("出现不明错误 !");
Response.End();
}
}
//数据绑定到Repeater
private void BindToRepeater(Repeater rp,int p)
{
XmlOp op = new XmlOp(_xmlFile);
ds = op.GetDs("//Root/Student[Name='" + _userName + "']");
P1_P2(p);
rp.DataSource = ds.Tables[0].DefaultView;
rp.DataBind();
}
//两个模板只显示一个
private void P1_P2(int i)
{
if (i == 1) //显示Repeater1
{
this.Repeater2.Visible = false;
this.Repeater1.Visible = true;
}
if (i == 2) //显示Repeater2
{
this.Repeater2.Visible = true;
this.Repeater1.Visible = false;
}
}
//修改信息按钮
protected void btnShowP2_Click(object sender, EventArgs e)
{
BindToRepeater(this.Repeater2, 2); //绑定Repeater2
}
//修改我的信息
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (IsValid) //通过验证
{
string _nickName = ((TextBox)this.Repeater2.Items[0].FindControl("txtNickName")).Text.Trim();
string _pwd = ((TextBox)this.Repeater2.Items[0].FindControl("txtPwd")).Text.Trim();
string _birthday = ((TextBox)this.Repeater2.Items[0].FindControl("txtBirthday")).Text.Trim();
string _qq = ((TextBox)this.Repeater2.Items[0].FindControl("txtQQ")).Text.Trim();
string _msn = ((TextBox)this.Repeater2.Items[0].FindControl("txtMsn")).Text.Trim();
string _Tel = ((TextBox)this.Repeater2.Items[0].FindControl("txtTel")).Text.Trim();
string _Email = ((TextBox)this.Repeater2.Items[0].FindControl("txtEmail")).Text.Trim();
string _address = ((TextBox)this.Repeater2.Items[0].FindControl("txtAddress")).Text.Trim();
string _work = ((TextBox)this.Repeater2.Items[0].FindControl("txtWork")).Text.Trim();
string _homepage = ((TextBox)this.Repeater2.Items[0].FindControl("txtHomepage")).Text.Trim();
string _photo = ((FileUpload)this.Repeater2.Items[0].FindControl("FileUploadImage")).FileName;
string _photoType = ((FileUpload)this.Repeater2.Items[0].FindControl("FileUploadImage")).PostedFile.ContentType;
string _time = DateTime.Now.ToString();
try
{
XmlOp op = new XmlOp(_xmlFile);
string _type = _photoType;
string _file = _photo.ToLower();
if (_photo != "") //如果没有选择图片时,就不需修改记录中的Photo属性
{
if (_photoType.Substring(0, 5) == "image")
{
//上传图片
_photo = "images/" + _userName + _file.Substring(_file.LastIndexOf("."));
((FileUpload)this.Repeater2.Items[0].FindControl("FileUploadImage")).SaveAs(Server.MapPath(_photo));
}
else
{
lg.Msg("图片格式不对!");
return;
}
string _xmlParentNode = "//Root/Student[Name='" + _userName + "']";
string[] _xmlNode ={ "NickName", "Pwd", "Birthday", "QQ", "Msn", "Tel", "Email", "Address", "Work", "Homepage", "Photo", "Time", };
string[] _xmlNodeTxt ={ _nickName, _pwd, _birthday, _qq, _msn, _Tel, _Email, _address, _work, _homepage, _photo, _time };
op.UpdateNode(_xmlParentNode, _xmlNode, _xmlNodeTxt);
}
else //选择了图片,需要修改记录中的Photo属性
{
string _xmlParentNode = "//Root/Student[Name='" + _userName + "']";
string[] _xmlNode ={ "NickName", "Pwd", "Birthday", "QQ", "Msn", "Tel", "Email", "Address", "Work", "Homepage", "Time", };
string[] _xmlNodeTxt ={ _nickName, _pwd, _birthday, _qq, _msn, _Tel, _Email, _address, _work, _homepage,_time };
op.UpdateNode(_xmlParentNode, _xmlNode, _xmlNodeTxt);
}
op.Save(_xmlFile); //保存新的信息
BindToRepeater(this.Repeater1,1); //重新绑定Repeater1控件
lg.Msg("你的登录密码已修改为 '" + _pwd + "'");
}
catch
{
lg.Msg("修改失败!");
}
}
}
//取消修改
protected void btnCancer_Click(object sender, EventArgs e)
{
P1_P2(1); //显示Repeater1,隐藏Repeater2
}
//绑定到前台照片照片显示的函数
public string Img(string img, string sex)
{
if (img == "") //没有图片时
{
if (sex == "男生")
return "img/boy.gif"; //显示默认的男生图片
else
return "img/girl.gif"; //显示默认的女生图片
}
else //有图片时
return img;
}
//绑定到前台当前登陆用户身份的函数
public string Admin(string admin)
{
if (admin == "yes") //是一般管理员
{
Session["NormalAdmin"] = "yes"; //将身份信息写入Session中
return "<a href='admin.aspx' target='_blank'>管理班级</a>";
}
else //不是一般管理员
return "非管理员";
}
//查找同学
protected void btnFind_Click(object sender, EventArgs e)
{
DataTable newDt = new DataTable(); //创建一个新的空表
DataTable dt = ((DataSet)Session["thisDS"]).Tables[0]; //读取Session中的表
newDt = dt.Clone(); //克隆表
DataRow[] dr = dt.Select("Name like '%" + this.txtStuName.Text + "%'"); //查找同学并放入数据行数组
for (int i = 0; i < dr.Length; i++)
{
newDt.ImportRow((DataRow)dr[i]); //重组新的数据表
}
this.DataGrid1.DataSource = newDt.DefaultView; //DataGrid1重新绑定
this.DataGrid1.DataBind();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -