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

📄 personinput.ascx.cs

📁 asp.net 2.0的教务管理软件源码
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
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;

public partial class uctl_PersonInput : System.Web.UI.UserControl
{
    public static int DoNothing = 0;
    public static int Added = 1;
    public static int Modified = 2;
    public static int Deleted = 3;
    private static string photoPath = "~/Personal/Photo/";
    public int Operation = DoNothing; //0-什么都没做 1-新增 2-修改 3-删除
    public String PersonName = "无名氏";
/*    string Company = "";
    string Email = "";
    string Address = "";
    string Tel = "";*/
    int pe_Sex = 0;
    int pe_Education = 0;
    int pe_Employment = 0;
    int pe_Region = 0;
    int id;
    string fileName;

    public int PersonID
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String sql = "SELECT * FROM J_Person WHERE pe_ID=" + id;
            DataTable dt = DBExec.ExecDataTable(sql);
            if (dt.Rows.Count != 0)
            {
                DataRow sdr = dt.Rows[0];
                txtName.Text = sdr["pe_Name"].ToString().Trim();
                if (sdr["pe_Birth"]!=DBNull.Value) txtBirth.Text = Convert.ToDateTime(sdr["pe_Birth"]).ToShortDateString();
                tpSex.SelectedValue = Convert.ToInt32(sdr["pe_Sex"]);
                tpRegion.SelectedValue = Convert.ToInt32(sdr["pe_Region"]);
                tpXueLi.SelectedValue = Convert.ToInt32(sdr["pe_Education"]);
                tpEmployment.SelectedValue = Convert.ToInt32(sdr["pe_Employment"]);
                PersonName = txtName.Text.Trim();
                txtTel.Text = sdr["pe_Tel"].ToString().Trim();
                txtEmail.Text = sdr["pe_Email"].ToString().Trim();
                txtAddress.Text = sdr["pe_Address"].ToString().Trim();
                txtCompany.Text = sdr["pe_Company"].ToString().Trim();
                
                if (sdr["pe_Portrait"].ToString().Trim() != "")
                    Image1.ImageUrl = photoPath + sdr["pe_Portrait"].ToString();
            }
        }
        else
        {
            pe_Sex = Convert.ToInt32(tpSex.SelectedValue);
            pe_Region = Convert.ToInt32(tpRegion.SelectedValue);
            pe_Education = Convert.ToInt32(tpXueLi.SelectedValue);
            pe_Employment = Convert.ToInt32(tpEmployment.SelectedValue);
        }
    }

    private bool upLoadPhoto()
    {
        Boolean fileOK = false;
        String fileExtension = "";
        String path = Server.MapPath(photoPath);
        if (filePhoto.HasFile) 
        {
            fileExtension = System.IO.Path.GetExtension(filePhoto.FileName).ToLower();
            String[] allowedExtensions = 
                {".gif", ".png", ".jpeg", ".jpg"};
          for (int i = 0; i < allowedExtensions.Length; i++)
          {
               if (fileExtension == allowedExtensions[i])
               {
                    fileOK = true;
               }
          }
        }

        if (fileOK)
        {
            try
            {
                fileName = id + fileExtension;
                System.IO.File.Delete(path + fileName);
                filePhoto.PostedFile.SaveAs(path + fileName);
                Label1.Text = "照片上传成功!";
                fileOK = true;
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
                fileOK = false;
            }
        }
        else
        {
            if (filePhoto.HasFile) Label1.Text = "照片能上传,可能文件格式不符合要求.<BR />只能上传gif/jpg/png/jpeg格式。";
            fileOK = false;
        }

        return fileOK;
    }

    private void ClearForm()
    {
        tpXueLi.SelectedValue = 0;
        tpSex.SelectedValue = 0;
        tpEmployment.SelectedValue = 0;
        tpRegion.SelectedValue = 0;
        txtAddress.Text = "";
        txtCompany.Text = "";
        txtEmail.Text = "";
        txtTel.Text = "";
        txtName.Text = "";
        txtBirth.Text = "";
        Image1.ImageUrl = "~/images/cache.gif";
    }

    public void AddorModify()
    {
        List<SqlParameter> sp = new List<SqlParameter>();
        sp.Add(new SqlParameter("@pe_Name", txtName.Text));
        sp.Add(new SqlParameter("@pe_Sex", pe_Sex));
        if (txtBirth.Text.Trim() !="")
            sp.Add(new SqlParameter("@pe_Birth", txtBirth.Text));
        else
            sp.Add(new SqlParameter("@pe_Birth", DBNull.Value));
        sp.Add(new SqlParameter("@pe_Education", tpXueLi.SelectedValue));
        sp.Add(new SqlParameter("@pe_Tel", txtTel.Text));
        sp.Add(new SqlParameter("@pe_Email", txtEmail.Text));
        sp.Add(new SqlParameter("@pe_Address",txtAddress.Text));
        sp.Add(new SqlParameter("@pe_Employment", tpEmployment.SelectedValue));
        sp.Add(new SqlParameter("@pe_Region", tpRegion.SelectedValue));
        sp.Add(new SqlParameter("@pe_Company", txtCompany.Text));
        PersonName = txtName.Text;
        if (id == 0)
        {
            DBExec.ExecNonQuery("p_AddPerson", sp);
            ClearForm();
            id = DBExec.ExecGetInt("SELECT IDENT_CURRENT('J_Person')");
            Operation = Added;
        }
        else
        {
            sp.Add(new SqlParameter("@pe_ID", id));
            DBExec.ExecNonQuery("p_ModifyPerson",sp);
            Operation = Modified;
        }

        if (upLoadPhoto())
        {
            DBExec.ExecNonQuery("UPDATE J_Person SET pe_Portrait='" + fileName.Trim() + "' WHERE pe_ID=" + id);
            Image1.ImageUrl = photoPath + fileName;
        }
    }

    public void Delete()
    {
        if (id > 0)
        {
            DataTable dt = DBExec.ExecDataTable("SELECT pe_ID, pe_Portrait FROM J_Person WHERE pe_ID="+id);
            if (dt.Rows.Count > 0)
            {
                DBExec.ExecNonQuery("DELETE FROM J_Person WHERE pe_ID=" + id);
                if (dt.Rows[0][1].ToString() != "")
                {
                    try
                    {
                        System.IO.File.Delete(Server.MapPath(photoPath + dt.Rows[0][1]));
                    }
                    catch { }
                }
                Image1.ImageUrl = "~/images/cache.gif";
            }
            Operation = Deleted;
            Response.Redirect(Request.Url.ToString(), true);
        }
        else
        {
        }
    }
}

⌨️ 快捷键说明

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