ÎÂܰÌáʾ£º´úÂëÔÚÏßä¯ÀÀ¹¦ÄÜÖ»ÄÜ×öΪԴÂëä¯ÀÀ²Î¿¼£¬²»ÄÜչʾÏîÄ¿µÄÈ«²¿£¬Èç¹ûÏë¸ü½øÒ»²½Á˽â¸Ã´úÂëÇëÏÂÔØ£ºXMLͬѧ¼ϵͳԴÂ루±ÏÒµÉè¼Æ£©
µ±Ç°Îļþ·¾¶£ºXMLClassBooks/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
13
using xmlOp; //ÒýÈëXmlOpÀàµÄÃüÃû¿Õ¼ä 14
15
public partial class Reg : System.Web.UI.Page 16
{ 17
string xmlFile = ConfigurationManager.AppSettings["xmlFile"]; //¶Á³öÅäÖÃÎļþµÄXmlÎļþ·¾¶ 18
Logic lg = new Logic(); //´´½¨LogicÀà¶ÔÏó 19
20
protected void Page_Load(object sender, EventArgs e) 21
{ 22
if (!IsPostBack) 23
{ 24
XmlOp op = new XmlOp(xmlFile); //´´½¨XmlOpÀà¶ÔÏó 25
if (op.NodeCount("//Root") == 0) //Èç¹ûÒ»¸öͬѧ³ÉÔ±¶¼Ã»ÓУ¬ÔòÈ¡ÏûÐÕÃûÑéÖ¤ 26
this.CustomValidator1.Enabled = false; 27
} 28
} 29
30
//Ìá½»×¢²áÐÅÏ¢ 31
protected void btnReg_Click(object sender, EventArgs e) 32
{ 33
if (IsValid) //Èç¹ûͨ¹ýËùÓÐÑéÖ¤ 34
{ 35
string _name = txtUserName.Text.Trim(); 36
string _nickName = txtNickName.Text.Trim(); 37
string _pwd = txtUserPwd.Text.Trim(); 38
string _sex = rbtnMan.Checked ? rbtnMan.Text : rbtnWoman.Text; //ÅжÏÐÔ±ð 39
string _birthday = txtBirthday.Text.Trim(); 40
string _email = txtEmail.Text.Trim(); 41
string _qq = txtQQ.Text.Trim(); 42
string _msn = txtMsn.Text.Trim(); 43
string _Tel = txtTel.Text.Trim(); 44
string _address = txtAddress.Text.Trim(); 45
string _work = txtWork.Text.Trim(); 46
string _homepage = txtHomePage.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/" + _name + _file.Substring(_file.LastIndexOf(".")); 58
this.FileUpload1.SaveAs(Server.MapPath(_photo)); //±£´æÍ¼Æ¬ 59
} 60
else 61
{ 62
lg.Msg("ͼƬ¸ñʽ²»¶Ô!"); 63
return; //ÖÕÖ¹Ö´ÐÐ 64
} 65
} 66
//½ÚµãÊý×é 67
string[] stu ={"Name", "NickName", "Pwd", "Sex", "Birthday", "Email", 68
"QQ", "Msn", "Tel", "Homepage", "Address", "Work", "Photo","Time" }; 69
//½ÚµãÖµÊý×é 70
string[] stuInfo ={_name, _nickName, _pwd, _sex, _birthday, _email, 71
_qq, _msn, _Tel, _homepage, _address, _work, _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='"+ _name +"']","Admin","no"); 78
op.Save(xmlFile); //±£´æXmlÎĵµ 79
lg.Msg("¹§Ï²,³É¹¦ÒÑ×¢²á!"); 80
ClearTextBox(); //Çå¿ÕËùÓÐÎı¾¿ò 81
} 82
} 83
} 84
85
//ÑéÖ¤Ãû×ÖÊÇ·ñ´æÔÚ 86
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 87
{ 88
string _name = args.Value; //È¡µÃÒªÑéÖ¤µÄÎı¾¿òÖµ 89
XmlOp op = new XmlOp(xmlFile); 90
if (op.SelectNode("//Root/Student", 0, _name)) //ÅжÏÊÇ·ñ´æÔÚ 91
args.IsValid = false; 92
else 93
args.IsValid = true; 94
} 95
96
//ÖØÐ´°´Å¥Ê¼þ 97
protected void btnReset_Click(object sender, EventArgs e) 98
{ 99
ClearTextBox(); //µ÷ÓÃÇå¿ÕÎı¾¿òº¯Êý 100
} 101
102
//Çå¿ÕÎı¾¿ò 103
private void ClearTextBox() 104
{ 105
this.txtAddress.Text = ""; 106
this.txtBirthday.Text = ""; 107
this.txtEmail.Text = ""; 108
this.txtTel.Text = ""; 109
this.txtHomePage.Text = ""; 110
this.txtMsn.Text = ""; 111
this.txtNickName.Text = ""; 112
this.txtQQ.Text = ""; 113
this.txtUserName.Text = ""; 114
this.txtUserPwd.Text = ""; 115
this.txtUserPwd2.Text = ""; 116
this.txtWork.Text = ""; 117
} 118
} 119





}
}