department.aspx.cs
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:13k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. using 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.Data.SqlClient;
  12. public partial class web_department_department : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         LoginDAO.CheckLogin(Session, Response, "../../", 2);
  17.         if (!IsPostBack)
  18.         {
  19.             Panel1.Visible = false;
  20.             Panel2.Visible = false;
  21.             Department_list.DataKeyNames = new string[] { "id" };
  22.             if(Request.QueryString["id"]!=null) 
  23.             {
  24.                 SqlConnection conn = dbConnection.getConnection();
  25.                 conn.Open();
  26.                 SqlCommand cmd = new SqlCommand("select name from OA_DEPARTMENT where id=" + Request.QueryString["id"], conn);
  27.                 string name = cmd.ExecuteScalar().ToString();
  28.                 lblTitle.Text = name + "下级部門列表";
  29.                 lblLast_department.Text = name;
  30.                 cmd = new SqlCommand("select count(id) from OA_EMPLOYEE where department_id="+Request.QueryString["id"], conn);
  31.                 int count = Convert.ToInt32(cmd.ExecuteScalar());
  32.                 if (count > 0)
  33.                 {
  34.                     btnShowEmployee.Text = "本部門員工(" + count + "人)";
  35.                 }
  36.                 else
  37.                 {
  38.                     btnShowEmployee.Text = "添加員工";
  39.                 }
  40.                 conn.Close();
  41.                 
  42.                 //顯示本部門資訊
  43.                 this.setDepartmentShow(Request.QueryString["id"].ToString());
  44.                 //選擇部門按鈕添加javascript
  45.                 this.btnSelect.Attributes.Add("onclick", "selectDepartment()");
  46.             }
  47.         }
  48.     }
  49.     protected void Department_list_RowDataBound(object sender, GridViewRowEventArgs e)
  50.     {
  51.         //行高亮
  52.         if (e.Row.RowType == DataControlRowType.DataRow)
  53.         {
  54.             e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#c8dafa';this.style.cursor='pointer';");
  55.             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
  56.         }
  57.     }
  58.     protected void Department_list_RowCommand(object sender, GridViewCommandEventArgs e)
  59.     {
  60.         string id = e.CommandArgument.ToString();
  61.         if (e.CommandName.Equals("Delete"))
  62.         {
  63.             if (!id.Equals("0"))
  64.             {
  65.                 SqlConnection conn = dbConnection.getConnection();
  66.                 conn.Open();
  67.                 // 检查该部门是否有子部门
  68.                 SqlCommand cmd = new SqlCommand("select count(id) from OA_DEPARTMENT where last_id=" + id, conn);
  69.                 int count = Convert.ToInt32(cmd.ExecuteScalar());
  70.                 if (count == 0)
  71.                 {
  72.                     SqlConnection con = dbConnection.getConnection();
  73.                     con.Open();
  74.                     SqlCommand sqlcom = new SqlCommand("select count(id) from OA_EMPLOYEE where dimission_date='' and department_id=" + id, con);
  75.                     int num = Convert.ToInt32(sqlcom.ExecuteScalar());
  76.                     if (num == 0)
  77.                     {
  78.                         //没有子部门, 直接删除
  79.                         cmd = new SqlCommand("delete from OA_DEPARTMENT where id=" + id, conn);
  80.                         cmd.ExecuteNonQuery();
  81.                     }
  82.                     else
  83.                     {
  84.                         Response.Write("<script language='javascript'>alert('该部门下有员工,不能删除!');</script>");
  85.                     }
  86.                     con.Close();
  87.                 }
  88.                 else
  89.                 {
  90.                     //有子部门, 提示先删除子部门
  91.                     Response.Write("<script language='javascript'>alert('请先删除下级部门, 再删除上级部门!');</script>");
  92.                 }
  93.                 conn.Close();
  94.             }
  95.             //刷新樹
  96.             Response.Write("<script language='javascript'>window.parent.frames.tree.location.reload()</script>");
  97.         }
  98.     }
  99.     protected void btnOk_Click(object sender, EventArgs e)
  100.     {
  101.         int flag = 0;
  102.         if (txtName.Text.Trim() == "")
  103.         {
  104.             flag = 1;
  105.             lblName.Text = "部門名稱不能為空!";
  106.         }
  107.         int last_id = 0;
  108.         if (Request.QueryString["id"] != null)
  109.         {
  110.             last_id = Convert.ToInt32(Request.QueryString["id"]);
  111.         }
  112.         //所有必填欄位都已填寫完畢
  113.         if (flag == 0)
  114.         {
  115.             SqlConnection conn = dbConnection.getConnection();
  116.             conn.Open();
  117.             SqlCommand cmd = new SqlCommand("select max(id) from OA_DEPARTMENT", conn);
  118.             int id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  119.             int sequence = 0;
  120.             if (last_id == 1)
  121.             {
  122.                 cmd = new SqlCommand("select max(sequence) from OA_DEPARTMENT where last_id="+last_id, conn);
  123.                 sequence = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  124.             }
  125.             string user_name = "";
  126.             try
  127.             {
  128.                 if (Session["user"] != null && Session["user"].ToString().Equals(""))
  129.                 {
  130.                     user_name = ((User)Session["user"]).Emp_name;
  131.                 }
  132.                 DateMgr mgr = new DateMgr();
  133.                 string time = mgr.getDateTime();
  134.                 string sql = "insert into OA_DEPARTMENT(id, name, code, tel, fax, [function], last_id, sequence,ceng,out)" +
  135.                     "values(" + id + ", '" + txtName.Text + "', '" + txtCode.Text + "', '" + txtTel.Text + "', '" + txtFax.Text + "', '" + txtFunction.Text +
  136.                     "'," + last_id + ", '" + sequence + "','"+textCeng.Text+"','"+Convert.ToInt32(DropDownList1.SelectedValue)+"')";
  137.                 cmd = new SqlCommand(sql, conn);
  138.                 cmd.ExecuteNonQuery();
  139.             }
  140.             catch (Exception ex)
  141.             {
  142.                 Response.Write(ex.Message);
  143.             }
  144.             conn.Close();
  145.             Department_list.DataBind();
  146.             txtName.Text = "";
  147.             txtCode.Text = "";
  148.             txtFax.Text = "";
  149.             txtTel.Text = "";
  150.             txtFunction.Text = "";
  151.         }
  152.         Panel1.Visible = false;
  153.         Panel2.Visible = false;
  154.         Panel3.Visible = false;
  155.         //刷新樹
  156.         Response.Write("<script language='javascript'>window.parent.frames.tree.location.reload()</script>");
  157.     }
  158.     protected void Department_list_RowUpdating(object sender, GridViewUpdateEventArgs e)
  159.     {
  160.         try
  161.         {
  162.             string id = Department_list.DataKeys[e.RowIndex].Value.ToString();
  163.             string name = ((TextBox)Department_list.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim().ToString();
  164.             string code = ((TextBox)Department_list.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim().ToString();
  165.             string tel = ((TextBox)Department_list.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim().ToString();
  166.             string fax = ((TextBox)Department_list.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim().ToString();
  167.             string function = ((TextBox)Department_list.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim().ToString();
  168.             string sql = "update OA_DEPARTMENT set name='" + name + "', code='" + code + "', tel='" + tel + "', " +
  169.                 "fax='" + fax + "', [function]='" + function + "' where id=" + id;
  170.             SqlConnection conn = dbConnection.getConnection();
  171.             conn.Open();
  172.             SqlCommand cmd = new SqlCommand(sql, conn);
  173.             cmd.ExecuteNonQuery();
  174.             conn.Close();
  175.             Department_list.DataBind();
  176.         }
  177.         catch (Exception ex)
  178.         {
  179.             Response.Write(ex.Message);
  180.         }
  181.     }
  182.     protected void btnEdit_Click(object sender, EventArgs e)
  183.     {
  184.         int flag = 0;
  185.         if (txtName2.Text.Trim() == "")
  186.         {
  187.             flag = 1;
  188.             lblName2.Text = "部門名稱不能為空!";
  189.         }
  190.         int id = 0;
  191.         if (Request.QueryString["id"] != null)
  192.         {
  193.             id = Convert.ToInt32(Request.QueryString["id"]);
  194.         }
  195.         //所有必填欄位都已填寫完畢
  196.         if (flag == 0)
  197.         {
  198.             try
  199.             {
  200.                 SqlConnection conn = dbConnection.getConnection();
  201.                 conn.Open();
  202.                 string sql = "update OA_DEPARTMENT set name='" + txtName2.Text + "', code='" + txtCode2.Text +
  203.                     "', tel='" + txtTel2.Text + "', fax='" + txtFax2.Text + "', [function]='" + txtFunction2.Text + "', last_id=" + lastDepartment_id.Value +
  204.                     ",ceng="+textCeng2.Text+",out='"+Convert.ToInt32(DropDownList2.SelectedValue)+"' where id=" + id;
  205.                 SqlCommand cmd = new SqlCommand(sql, conn);
  206.                 cmd.ExecuteNonQuery();
  207.                 conn.Close();
  208.                 lblTitle.Text = txtName2.Text + "下级部門列表";
  209.             }
  210.             catch (Exception ex)
  211.             {
  212.                 Response.Write(ex.Message);
  213.             }
  214.         }
  215.         Panel1.Visible = false;
  216.         Panel2.Visible = false;
  217.         Panel3.Visible = true;
  218.         this.setDepartmentShow(id.ToString());
  219.         //刷新樹
  220.         Response.Write("<script language='javascript'>window.parent.frames.tree.location.reload()</script>");
  221.     }
  222.     protected void btnAddNew_Click(object sender, EventArgs e)
  223.     {
  224.         Panel2.Visible = false;
  225.         Panel1.Visible = true;
  226.         Panel3.Visible = false;
  227.     }
  228.     protected void btnEditDepartment_Click(object sender, EventArgs e)
  229.     {
  230.         Panel2.Visible = true;
  231.         Panel1.Visible = false;
  232.         Panel3.Visible = false;
  233.         if (Request.QueryString["id"] != null)
  234.         {
  235.             try
  236.             {
  237.                 string id = Request.QueryString["id"].ToString();
  238.                 SqlConnection conn = dbConnection.getConnection();
  239.                 conn.Open();
  240.                 SqlCommand cmd = new SqlCommand("select last_id, name, code, tel, fax, [function],ceng from OA_DEPARTMENT where id="+id, conn);
  241.                 int last_id = 0;
  242.                 SqlDataReader dr = cmd.ExecuteReader();
  243.                 if (dr.Read())
  244.                 {
  245.                     last_id = Convert.ToInt32(dr["last_id"]);
  246.                     lastDepartment_id.Value = last_id.ToString();
  247.                     txtName2.Text = dr["name"].ToString();
  248.                     txtCode2.Text = dr["code"].ToString();
  249.                     txtTel2.Text = dr["tel"].ToString();
  250.                     txtFax2.Text = dr["fax"].ToString();
  251.                     txtFunction2.Text = dr["function"].ToString();
  252.                     textCeng2.Text=dr["ceng"].ToString();
  253.                 }
  254.                 dr.Close();
  255.                 
  256.                 cmd = new SqlCommand("select name from OA_DEPARTMENT where id=" + last_id, conn);
  257.                 txtLastDepartment2.Text = cmd.ExecuteScalar().ToString();
  258.                 conn.Close();
  259.             }
  260.             catch (Exception ex)
  261.             {
  262.                 Response.Write(ex.Message);
  263.             }
  264.         }
  265.     }
  266.     public void setDepartmentShow(string id)
  267.     {
  268.         try
  269.         {
  270.             SqlConnection conn = dbConnection.getConnection();
  271.             conn.Open();
  272.             SqlCommand cmd = new SqlCommand("select name, code, last_id, tel, fax, [function],ceng,out from OA_DEPARTMENT where id="+id, conn);
  273.             SqlDataReader dr = cmd.ExecuteReader();
  274.             int last_id = 0;
  275.             if (dr.Read())
  276.             {
  277.                 lblDepartmentName.Text = dr["name"].ToString();
  278.                 lblDepartmentCode.Text = dr["code"].ToString();
  279.                 last_id = Convert.ToInt32(dr["last_id"]);
  280.                 lblTel.Text = dr["tel"].ToString();
  281.                 lblFax.Text = dr["fax"].ToString();
  282.                 lblFunction.Text = dr["function"].ToString();
  283.                 lblCeng.Text = dr["ceng"].ToString();
  284.                 int var = Convert.ToInt32(dr["out"]);
  285.                 if (var == 1)
  286.                 {
  287.                     lblcompany.Text = "津味";
  288.                 }
  289.                 else if (var == 2)
  290.                 {
  291.                     lblcompany.Text = "和夏";
  292.                 }
  293.                 else
  294.                 {
  295.                     lblcompany.Text = "";
  296.                 }
  297.             }
  298.             dr.Close();
  299.             cmd = new SqlCommand("select name from OA_DEPARTMENT where id="+last_id, conn);
  300.             lblLastDepartment.Text = cmd.ExecuteScalar().ToString();
  301.             conn.Close();
  302.         }
  303.         catch (Exception ex)
  304.         {
  305.             Response.Write(ex.Message);
  306.         }
  307.     }
  308.     protected void btnShow_Click(object sender, EventArgs e)
  309.     {
  310.         Panel1.Visible = false;
  311.         Panel2.Visible = false;
  312.         Panel3.Visible = true;
  313.     }
  314.     protected void btnShowEmployee_Click(object sender, EventArgs e)
  315.     {
  316.         if (Request.QueryString["id"] != null)
  317.         {
  318.             Response.Redirect("../employee/employee_list.aspx?department_id="+Request.QueryString["id"]+"&department=true");
  319.         }
  320.     }
  321. }