📄 reg.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 Reg : System.Web.UI.Page
{
string xmlFile = ConfigurationManager.AppSettings["xmlFile"]; //读出配置文件的Xml文件路径
Logic lg = new Logic(); //创建Logic类对象
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XmlOp op = new XmlOp(xmlFile); //创建XmlOp类对象
if (op.NodeCount("//Root") == 0) //如果一个同学成员都没有,则取消姓名验证
this.CustomValidator1.Enabled = false;
}
}
//提交注册信息
protected void btnReg_Click(object sender, EventArgs e)
{
if (IsValid) //如果通过所有验证
{
string _name = txtUserName.Text.Trim();
string _nickName = txtNickName.Text.Trim();
string _pwd = txtUserPwd.Text.Trim();
string _sex = rbtnMan.Checked ? rbtnMan.Text : rbtnWoman.Text; //判断性别
string _birthday = txtBirthday.Text.Trim();
string _email = txtEmail.Text.Trim();
string _qq = txtQQ.Text.Trim();
string _msn = txtMsn.Text.Trim();
string _Tel = txtTel.Text.Trim();
string _address = txtAddress.Text.Trim();
string _work = txtWork.Text.Trim();
string _homepage = txtHomePage.Text.Trim();
string _photo = "";
string _time = DateTime.Now.ToString();
//上传图片
string _file = this.FileUpload1.FileName.ToString().ToLower();
if (_file != "")
{
string _fileType = this.FileUpload1.PostedFile.ContentType;
if (_fileType.Substring(0, 5) == "image") //判断是否图片文件
{
_photo = "images/" + _name + _file.Substring(_file.LastIndexOf("."));
this.FileUpload1.SaveAs(Server.MapPath(_photo)); //保存图片
}
else
{
lg.Msg("图片格式不对!");
return; //终止执行
}
}
//节点数组
string[] stu ={"Name", "NickName", "Pwd", "Sex", "Birthday", "Email",
"QQ", "Msn", "Tel", "Homepage", "Address", "Work", "Photo","Time" };
//节点值数组
string[] stuInfo ={_name, _nickName, _pwd, _sex, _birthday, _email,
_qq, _msn, _Tel, _homepage, _address, _work, _photo,_time };
XmlOp op = new XmlOp(xmlFile);
//调用Xml操作类的InsertNode方法,插入新的同学成员记录
if (op.InsertNode("//Root", "Student", stu, stuInfo))
{
op.InsertAttrib("//Root/Student[Name='"+ _name +"']","Admin","no");
op.Save(xmlFile); //保存Xml文档
lg.Msg("恭喜,成功已注册!");
ClearTextBox(); //清空所有文本框
}
}
}
//验证名字是否存在
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string _name = args.Value; //取得要验证的文本框值
XmlOp op = new XmlOp(xmlFile);
if (op.SelectNode("//Root/Student", 0, _name)) //判断是否存在
args.IsValid = false;
else
args.IsValid = true;
}
//重写按钮事件
protected void btnReset_Click(object sender, EventArgs e)
{
ClearTextBox(); //调用清空文本框函数
}
//清空文本框
private void ClearTextBox()
{
this.txtAddress.Text = "";
this.txtBirthday.Text = "";
this.txtEmail.Text = "";
this.txtTel.Text = "";
this.txtHomePage.Text = "";
this.txtMsn.Text = "";
this.txtNickName.Text = "";
this.txtQQ.Text = "";
this.txtUserName.Text = "";
this.txtUserPwd.Text = "";
this.txtUserPwd2.Text = "";
this.txtWork.Text = "";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -