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

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_main : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         LoginDAO.CheckLogin(Session, Response, "../../", 2);
  17.         int row = Department_list.Rows.Count;
  18.         if (Request.QueryString["act"] != null &&  Request.QueryString["id"]!=null)
  19.         {
  20.             this.updateSequence(Request.QueryString["act"].ToString(), Convert.ToInt32(Request.QueryString["id"]));
  21.         }
  22.         HyperLink link = (HyperLink)Department_list.Rows[0].Cells[1].FindControl("linkUp");
  23.         link.Visible = false;
  24.         link = (HyperLink)Department_list.Rows[row - 1].Cells[1].FindControl("linkDown");
  25.         link.Visible = false;
  26.         for (int i = 0; i < row; i++)
  27.         {
  28.             String id = Department_list.Rows[i].Cells[2].Text;
  29.             HyperLink link_u = (HyperLink)Department_list.Rows[i].Cells[1].FindControl("linkUp");
  30.             link_u.NavigateUrl = "department_main.aspx?act=up&id=" + id;
  31.             HyperLink link_d = (HyperLink)Department_list.Rows[i].Cells[1].FindControl("linkDown");
  32.             link_d.NavigateUrl = "department_main.aspx?act=down&id=" + id;
  33.         }
  34.         //Department_list.Columns[2].Visible = false;
  35.         if (!IsPostBack)
  36.         {
  37.             SqlConnection conn = dbConnection.getConnection();
  38.             conn.Open();
  39.             SqlCommand cmd = new SqlCommand("select name from OA_DEPARTMENT where id=1", conn);
  40.             string name = cmd.ExecuteScalar().ToString();
  41.             lblLast_department.Text = name;
  42.         }
  43.     }
  44.     //更新部門排序
  45.     public void updateSequence(string act, int id)
  46.     {
  47.         try
  48.         {
  49.             SqlConnection conn = dbConnection.getConnection();
  50.             conn.Open();
  51.             int sequence = 0;
  52.             SqlCommand cmd = new SqlCommand("select sequence from OA_DEPARTMENT where id="+id, conn);
  53.             sequence = Convert.ToInt32(cmd.ExecuteScalar());
  54.             if(act.Equals("up"))
  55.             {
  56.                 cmd = new SqlCommand("update OA_DEPARTMENT set sequence=sequence+1 where sequence=" + (sequence - 1), conn);
  57.                 cmd.ExecuteNonQuery();
  58.                 cmd = new SqlCommand("update OA_DEPARTMENT set sequence=sequence-1 where id=" + id, conn);
  59.                 cmd.ExecuteNonQuery();
  60.             }
  61.             else
  62.             {
  63.                 cmd = new SqlCommand("update OA_DEPARTMENT set sequence=sequence-1 where sequence=" + (sequence + 1), conn);
  64.                 cmd.ExecuteNonQuery();
  65.                 cmd = new SqlCommand("update OA_DEPARTMENT set sequence=sequence+1 where id=" + id, conn);
  66.                 cmd.ExecuteNonQuery();
  67.             }
  68.             conn.Close();
  69.             SqlDataSource1.SelectCommand = "select id, name from OA_DEPARTMENT where last_id=1 order by sequence";
  70.             Department_list.DataBind();
  71.         }
  72.         catch(Exception e)
  73.         {
  74.             Response.Write(e.Message);
  75.         }
  76.     }
  77.     protected void Department_list_RowDataBound(object sender, GridViewRowEventArgs e)
  78.     {
  79.         //判断是否是DataRow,以防止鼠标经过Header也有效果  
  80.         if (e.Row.RowType == DataControlRowType.DataRow)
  81.         {
  82.             e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#c8dafa';");
  83.             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
  84.             //删除键在最后一列
  85.             e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "DeleteConfirm(event)");
  86.         }
  87.     }
  88.     protected void btnOk_Click(object sender, EventArgs e)
  89.     {
  90.         int flag = 0;
  91.         if (txtName.Text == "")
  92.         {
  93.             flag = 1;
  94.             lblName.Text = "部門名稱不能為空";
  95.         }
  96.         //所有數據已填完, 添加部門
  97.         if (flag == 0)
  98.         {
  99.             SqlConnection conn = dbConnection.getConnection();
  100.             conn.Open();
  101.             SqlCommand cmd = new SqlCommand("select max(id) from OA_DEPARTMENT", conn);
  102.             int id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  103.             int sequence = 0;
  104.             cmd = new SqlCommand("select max(sequence) from OA_DEPARTMENT where last_id=1", conn);
  105.             sequence = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  106.             string user_name = "";
  107.             try
  108.             {
  109.                 if (Session["user"] != null && Session["user"].ToString().Equals(""))
  110.                 {
  111.                     user_name = ((User)Session["user"]).Emp_name;
  112.                 } 
  113.                 DateMgr mgr = new DateMgr();
  114.                 string time = mgr.getDateTime();
  115.                 string sql = "insert into OA_DEPARTMENT(id, name, code, tel, fax, [function], last_id, sequence, cre_user, cre_date)" +
  116.                     "values(" + id + ", '" + txtName.Text + "', '" + txtCode.Text + "', '" + txtTel.Text + "', '" + txtFax.Text + "', '" + txtFunction.Text +
  117.                     "'," + 1 + ", " + sequence + ", '" + user_name + "', '" + time + "')";
  118.                 cmd = new SqlCommand(sql, conn);
  119.                 cmd.ExecuteNonQuery();
  120.             }
  121.             catch (Exception ex)
  122.             {
  123.                 Response.Write(ex.Message);
  124.             }
  125.             conn.Close();
  126.             Department_list.DataBind();
  127.             txtName.Text = "";
  128.             txtCode.Text = "";
  129.             txtFax.Text = "";
  130.             txtTel.Text = "";
  131.             txtFunction.Text = "";
  132.             //刷新樹
  133.             Response.Write("<script language='javascript'>window.parent.frames.tree.location.reload()</script>");
  134.         }
  135.     }
  136. }