⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reg.aspx.cs

📁 简单的XML学生信息系统
💻 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 System.Xml;
//该源码下载自www.51aspx.com(51aspx.com)

using xmlOp;//引用自定义命名空间

public partial class reg : System.Web.UI.Page
{
    string xmlFile = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        xmlFile = Request.PhysicalApplicationPath + @"\xml\students.xml";//配置文件的Xml文件路径
        if (!IsPostBack)
        {
            XmlOp op = new XmlOp(xmlFile);  //创建XmlOp类对象
            if (op.NodeCount("//Root") == 0) //如果一个同学成员都没有,则取消姓名验证
                this.CustomValidator1.Enabled = false;
        }
    }
    //提交按钮事件
    protected void bt1_Click(object sender, EventArgs e)
    {
        if (IsValid)//如果通过所有验证
        {
            string _username = tbusername.Text.Trim();
            string _pwd = tbpassword1.Text.Trim();
            string _sex = rdboy.Checked ? rdboy.Text : rbgirl.Text;//判断性别 
            string _email = tbemail.Text.Trim();
            string _dept = tbdept.Text.Trim();
            string _class = tbclass.Text.Trim();
            string _birthday = tbbirthday.Text.Trim();
            string _addrees = tbaddrees.Text.Trim();
            string _postcode = tbpostcode.Text.Trim();
            string _telephone = tbtelephone.Text.Trim();
            string _stuid = tbstuid.Text.Trim();
            string _sub = tbsub.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/" + _username + _file.Substring(_file.LastIndexOf("."));
                    this.FileUpload1.SaveAs(Server.MapPath(_photo)); //保存图片
                }
                else
                {
                    Msg("对不起!图片格式不对!");
                    return; 
                }
            }
            //定义节点数组
            string[] stu ={"Name", "Pwd", "Sex", "Email", "Dept", "Class", "Birthday", "Address", 
                           "Postcode", "Tel", "Stuid", "Sub", "Photo","Time"};
            //定义节点值数组
            string[] stuinfo ={_username, _pwd, _sex, _email, _dept, _class, _birthday, _addrees,
                               _postcode, _telephone, _stuid, _sub, _photo,_time };

            XmlOp op = new XmlOp(xmlFile);
            //调用Xml操作类的InsertNode方法,插入新的同学成员记录
            if (op.InsertNode("//Root", "Student", stu, stuinfo))
            {
                op.InsertAttrib("//Root/Student[Name='" + _username + "']", "Admin", "no");
                op.Save(xmlFile);   //保存Xml文档
                Msg("恭喜,成功已注册!");
                ClearTextBox(); //清空所有文本框
            } 
        }
    }
    //验证用户名是否存在
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string _username = args.Value;
        XmlOp op = new XmlOp(xmlFile);
        if (op.SelectNode("//Root/Student", 0, _username))  //判断是否存在
            args.IsValid = false;
        else
            args.IsValid = true;
    }
    //重置按钮事件
    protected void bt2_Click(object sender, EventArgs e)
    {
        ClearTextBox();//调用清空文本框函数
    }
    //清空文本框
    private void ClearTextBox()
    {
        tbusername.Text = "";
        tbpassword1.Text = "";
        tbpassword2.Text = "";
        tbemail.Text = "";
        tbdept.Text = "";
        tbclass.Text = "";
        tbbirthday.Text = "";
        tbaddrees.Text = "";
        tbpostcode.Text = "";
        tbtelephone.Text = "";
        tbstuid.Text = "";
        tbsub.Text = "";
    }
    //弹出对话框
    public void Msg(string msg)
    {
        Page pg = (Page)HttpContext.Current.Handler;
        string script = "alert('" + msg + "');";
        pg.ClientScript.RegisterStartupScript(pg.GetType(), Guid.NewGuid().ToString(), script, true);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -