温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:简单的XML学生信息系统(注释清楚)
当前文件路径:XmlStudentSys/reg.aspx.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.IO; 12
using System.Xml; 13
//该源码下载自www.51aspx.com(51aspx.com) 14
15
using xmlOp;//引用自定义命名空间 16
17
public partial class reg : System.Web.UI.Page 18
{ 19
string xmlFile = ""; 20
protected void Page_Load(object sender, EventArgs e) 21
{ 22
xmlFile = Request.PhysicalApplicationPath + @"\xml\students.xml";//配置文件的Xml文件路径 23
if (!IsPostBack) 24
{ 25
XmlOp op = new XmlOp(xmlFile); //创建XmlOp类对象 26
if (op.NodeCount("//Root") == 0) //如果一个同学成员都没有,则取消姓名验证 27
this.CustomValidator1.Enabled = false; 28
} 29
} 30
//提交按钮事件 31
protected void bt1_Click(object sender, EventArgs e) 32
{ 33
if (IsValid)//如果通过所有验证 34
{ 35
string _username = tbusername.Text.Trim(); 36
string _pwd = tbpassword1.Text.Trim(); 37
string _sex = rdboy.Checked ? rdboy.Text : rbgirl.Text;//判断性别 38
string _email = tbemail.Text.Trim(); 39
string _dept = tbdept.Text.Trim(); 40
string _class = tbclass.Text.Trim(); 41
string _birthday = tbbirthday.Text.Trim(); 42
string _addrees = tbaddrees.Text.Trim(); 43
string _postcode = tbpostcode.Text.Trim(); 44
string _telephone = tbtelephone.Text.Trim(); 45
string _stuid = tbstuid.Text.Trim(); 46
string _sub = tbsub.Text.Trim(); 47
string _photo = ""; 48
string _time = DateTime.Now.ToString(); 49
50
//上传图片 51
string _file = this.FileUpload1.FileName.ToString().ToLower(); 52
if (_file != "") 53
{ 54
string _fileType = this.FileUpload1.PostedFile.ContentType; 55
if (_fileType.Substring(0, 5) == "image") //判断是否图片文件 56
{ 57
_photo = "images/" + _username + _file.Substring(_file.LastIndexOf(".")); 58
this.FileUpload1.SaveAs(Server.MapPath(_photo)); //保存图片 59
} 60
else 61
{ 62
Msg("对不起!图片格式不对!"); 63
return; 64
} 65
} 66
//定义节点数组 67
string[] stu ={"Name", "Pwd", "Sex", "Email", "Dept", "Class", "Birthday", "Address", 68
"Postcode", "Tel", "Stuid", "Sub", "Photo","Time"}; 69
//定义节点值数组 70
string[] stuinfo ={_username, _pwd, _sex, _email, _dept, _class, _birthday, _addrees, 71
_postcode, _telephone, _stuid, _sub, _photo,_time }; 72
73
XmlOp op = new XmlOp(xmlFile); 74
//调用Xml操作类的InsertNode方法,插入新的同学成员记录 75
if (op.InsertNode("//Root", "Student", stu, stuinfo)) 76
{ 77
op.InsertAttrib("//Root/Student[Name='" + _username + "']", "Admin", "no"); 78
op.Save(xmlFile); //保存Xml文档 79
Msg("恭喜,成功已注册!"); 80
ClearTextBox(); //清空所有文本框 81
} 82
} 83
} 84
//验证用户名是否存在 85
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 86
{ 87
string _username = args.Value; 88
XmlOp op = new XmlOp(xmlFile); 89
if (op.SelectNode("//Root/Student", 0, _username)) //判断是否存在 90
args.IsValid = false; 91
else 92
args.IsValid = true; 93
} 94
//重置按钮事件 95
protected void bt2_Click(object sender, EventArgs e) 96
{ 97
ClearTextBox();//调用清空文本框函数 98
} 99
//清空文本框 100
private void ClearTextBox() 101
{ 102
tbusername.Text = ""; 103
tbpassword1.Text = ""; 104
tbpassword2.Text = ""; 105
tbemail.Text = ""; 106
tbdept.Text = ""; 107
tbclass.Text = ""; 108
tbbirthday.Text = ""; 109
tbaddrees.Text = ""; 110
tbpostcode.Text = ""; 111
tbtelephone.Text = ""; 112
tbstuid.Text = ""; 113
tbsub.Text = ""; 114
} 115
//弹出对话框 116
public void Msg(string msg) 117
{ 118
Page pg = (Page)HttpContext.Current.Handler; 119
string script = "alert('" + msg + "');"; 120
pg.ClientScript.RegisterStartupScript(pg.GetType(), Guid.NewGuid().ToString(), script, true); 121
} 122
} 123





}
}