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

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_announcement_announce_mgr_list : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         if (!IsPostBack)
  17.         {
  18.             AnnounceList.DataKeyNames = new string[] { "id" };
  19.             if (Request.QueryString["type_id"] != null && Request.QueryString["type"] != null)
  20.             {
  21.                 int type_id = Convert.ToInt32(Request.QueryString["type_id"]);
  22.                 int type = Convert.ToInt32(Request.QueryString["type"]);
  23.                 SqlConnection conn = dbConnection.getConnection();
  24.                 conn.Open();
  25.                 SqlCommand cmd = new SqlCommand("select name from OA_ANNOUNCEMENT_TYPE where id="+type_id, conn);
  26.                 LblTitle.Text = cmd.ExecuteScalar().ToString();
  27.                 conn.Close();
  28.                 DateMgr mgr = new DateMgr();
  29.                 string today = mgr.getDate();
  30.                 string sql = "";
  31.                 if (type == 1)
  32.                 {
  33.                     LblTitle.Text = LblTitle.Text + "(正在發佈)";
  34.                     sql = "select id, subject, cre_user, cre_date from OA_ANNOUNCEMENT where type_id=" +
  35.                         type_id + " and is_del='N' and validate_date>='" + today + "' order by cre_date desc";
  36.                 }
  37.                 if (type == 2)
  38.                 {
  39.                     LblTitle.Text = LblTitle.Text + "(已過期)";
  40.                     sql = "select id, subject, cre_user, cre_date from OA_ANNOUNCEMENT where type_id=" +
  41.                         type_id + " and is_del='N' and validate_date<='" + today + "' order by cre_date desc";
  42.                 }
  43.                 if (type == 3)
  44.                 {
  45.                     LblTitle.Text = LblTitle.Text + "(已刪除)";
  46.                     sql = "select id, subject, cre_user, cre_date from OA_ANNOUNCEMENT where type_id=" +
  47.                         type_id + " and is_del='Y' order by cre_date desc";
  48.                 }
  49.                 SqlDataSource1.SelectCommand = sql;
  50.                 AnnounceList.DataBind();
  51.                 if (type == 3)
  52.                 {
  53.                     AnnounceList.Columns[AnnounceList.Columns.Count - 1].Visible = false;
  54.                     AnnounceList.Columns[AnnounceList.Columns.Count - 2].Visible = true;
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     protected void AnnounceList_RowDataBound(object sender, GridViewRowEventArgs e)
  60.     {
  61.         //行高亮
  62.         if (e.Row.RowType == DataControlRowType.DataRow)
  63.         {
  64.             LblTitle.Visible = true;
  65.             e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#c8dafa';");
  66.             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
  67.             e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "return confirm('確定删除嗎?')");
  68.             e.Row.Cells[e.Row.Cells.Count - 2].Attributes.Add("onclick", "return confirm('確定還原嗎?')");
  69.         }
  70.     }
  71.     protected void AnnounceList_RowCommand(object sender, GridViewCommandEventArgs e)
  72.     {
  73.         if (e.CommandName.Equals("EditData"))
  74.         {
  75.             string id = e.CommandArgument.ToString();
  76.             Session["id"] = id;
  77.             this.SetValue(id);
  78.             PnlAnnounce.Visible = true;
  79.         }
  80.         if (e.CommandName.Equals("Restore"))
  81.         {
  82.             string id = e.CommandArgument.ToString();
  83.             SqlConnection conn = dbConnection.getConnection();
  84.             conn.Open();
  85.             SqlCommand cmd = new SqlCommand("update OA_ANNOUNCEMENT set is_del='N' where id=" + id, conn);
  86.             cmd.ExecuteNonQuery();
  87.             conn.Close();
  88.             AnnounceList.DataBind();
  89.             //刷新樹
  90.             Response.Write("<script language='javascript'>window.parent.frames.location.reload()</script>");
  91.         }
  92.     }
  93.     protected void AnnounceList_RowCreated(object sender, GridViewRowEventArgs e)
  94.     {
  95.         if (e.Row.RowType == DataControlRowType.DataRow)
  96.         {
  97.             LinkButton btn = (LinkButton)e.Row.FindControl("LbEdit");
  98.             btn.CommandArgument = AnnounceList.DataKeys[e.Row.RowIndex].Value.ToString();
  99.             btn = (LinkButton)e.Row.FindControl("LbRestore");
  100.             btn.CommandArgument = AnnounceList.DataKeys[e.Row.RowIndex].Value.ToString();
  101.         }
  102.     }
  103.     protected void SetValue(string id)
  104.     {
  105.         SqlConnection conn = dbConnection.getConnection();
  106.         conn.Open();
  107.         SqlCommand cmd = new SqlCommand("select type_id, subject, content, authority, validate_date, " +
  108.             "attachment, attachment_save_name from OA_ANNOUNCEMENT where id=" + id, conn);
  109.         SqlDataReader dr = cmd.ExecuteReader();
  110.         int flag = 0;
  111.         if (dr.Read())
  112.         {
  113.             DdlType.SelectedValue = dr["type_id"].ToString();
  114.             TxtSubject.Text = dr["subject"].ToString();
  115.             TxtContent.Text = dr["content"].ToString().Replace("<br>", "n").Replace("&nbsp;", " ");
  116.             RbAuthority.SelectedValue = dr["authority"].ToString();
  117.             TxtValidateDate.Text = dr["validate_date"].ToString();
  118.             if (RbAuthority.SelectedValue.Equals("2"))
  119.             {
  120.                 flag = 1;
  121.             }
  122.             
  123.             LbAttachment.Text = dr["attachment"].ToString();
  124.             AttachmentSaveName.Value = dr["attachment_save_name"].ToString();
  125.             if (LbAttachment.Text.Equals(""))
  126.             {
  127.                 BtnDelete.Visible = false;
  128.                 FileUpload1.Visible = true;
  129.             }
  130.         }
  131.         dr.Close();
  132.         if (flag == 1)
  133.         {
  134.             BtnSelect.Visible = true;
  135.             LblDepartments.Visible = true;
  136.             cmd = new SqlCommand("select a.department_id, d.name department from OA_ANNOUNCEMENT_DEPARTMENT a " +
  137.                 "inner join OA_DEPARTMENT d on a.department_id=d.id where a_id=" + id, conn);
  138.             dr = cmd.ExecuteReader();
  139.             string departments = "";
  140.             string department_ids = "";
  141.             while (dr.Read())
  142.             {
  143.                 department_ids = department_ids + dr["department_id"].ToString() + ";";
  144.                 departments = departments + dr["department"].ToString() + ";";
  145.             }
  146.             if (department_ids.Length >= 1)
  147.             {
  148.                 DepartmentIds.Value = department_ids.Substring(0, department_ids.Length - 1);
  149.                 LblDepartments.Text = departments.Substring(0, departments.Length - 1);
  150.             }
  151.             dr.Close();
  152.         }
  153.         conn.Close();
  154.     }
  155.     protected bool uploadFile(string time)
  156.     {
  157.         bool flag = false;
  158.         string directory = "d:\oa(new)\web\oa_upload\announce\";
  159.         if (FileUpload1.HasFile)
  160.         {
  161.             bool extenstion = false;
  162.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  163.             string[] allowedExtensions = 
  164.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  165.             for (int i = 0; i < allowedExtensions.Length; i++)
  166.             {
  167.                 if (fileExtension == allowedExtensions[i])
  168.                 {
  169.                     extenstion = true;
  170.                     break;
  171.                 }
  172.             }
  173.             if (!extenstion)
  174.             {
  175.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
  176.                 return false;
  177.             }
  178.             else
  179.             {
  180.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  181.                 FileUpload1.PostedFile.SaveAs(directory + name);
  182.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  183.                 flag = true;
  184.             }
  185.         }
  186.         else
  187.         {
  188.             flag = true;
  189.         }
  190.         return flag;
  191.     }
  192.     protected void LbAttachment_Click(object sender, EventArgs e)
  193.     {
  194.         this.DownLoadFile(AttachmentSaveName.Value);
  195.     }
  196.     protected void DownLoadFile(string name)
  197.     {
  198.         string directory = "..\oa_upload\announce\";
  199.         try
  200.         {
  201.             Response.Clear();
  202.             Response.ClearHeaders();
  203.             Response.Buffer = false;
  204.             Response.ContentType = "application/octet-stream";
  205.             Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(LbAttachment.Text, System.Text.Encoding.UTF8));
  206.             Response.AppendHeader("Content-Length", ContentLength.Value);
  207.             Response.WriteFile(directory + name);
  208.             Response.Flush();
  209.             Response.End();
  210.         }
  211.         catch (Exception ex)
  212.         {
  213.             Response.Write(ex.Message);
  214.         }
  215.     }
  216.     protected void AnnounceList_RowDeleted(object sender, GridViewDeletedEventArgs e)
  217.     {
  218.         //刷新樹
  219.         Response.Write("<script language='javascript'>window.parent.frames.location.reload()</script>");
  220.         AnnounceList.DataBind();
  221.     }
  222.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  223.     {
  224.         string year = Calendar1.SelectedDate.Year.ToString();
  225.         string month = Calendar1.SelectedDate.Month.ToString();
  226.         if (month.Length == 1)
  227.         {
  228.             month = "0" + month;
  229.         }
  230.         string day = Calendar1.SelectedDate.Day.ToString();
  231.         if (day.Length == 1)
  232.         {
  233.             day = "0" + day;
  234.         }
  235.         TxtValidateDate.Text = year + "-" + month + "-" + day;
  236.         Calendar1.Visible = false;
  237.     }
  238.     protected void ImageButton1_Click(object sender, EventArgs e)
  239.     {
  240.         if (Calendar1.Visible == true)
  241.         {
  242.             Calendar1.Visible = false;
  243.         }
  244.         else
  245.         {
  246.             Calendar1.Visible = true;
  247.         }
  248.     }
  249.     protected void BtnOk_Click(object sender, EventArgs e)
  250.     {
  251.         
  252.             DateMgr mgr = new DateMgr();
  253.             string time = mgr.getTime();
  254.             string user = "";
  255.             if (Session["user"] != null)
  256.             {
  257.                 user = ((User)Session["user"]).Emp_name;
  258.             }
  259.             if (this.uploadFile(time))
  260.             {
  261.                 string file_name = "";
  262.                 string save_name = "";
  263.                 if (FileUpload1.HasFile)
  264.                 {
  265.                     file_name = FileUpload1.FileName;
  266.                     save_name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  267.                 }
  268.                 SqlConnection conn = dbConnection.getConnection();
  269.                 conn.Open();            
  270.                              
  271.              SqlCommand   cmd=new SqlCommand("update OA_ANNOUNCEMENT set type_id='" + DdlType.SelectedValue + "', subject='" + TxtSubject.Text +
  272.                 "', content='" + TxtContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp;") + "', authority='" + RbAuthority.SelectedValue +
  273.                 "',validate_date='" + TxtValidateDate.Text + "',attachment='" + file_name + "',attachment_save_name='" + save_name +
  274.                 "',cre_date='" + mgr.getDateTime() + "' where id=" + Session["id"], conn);
  275.                  cmd.ExecuteNonQuery();
  276.                 if (RbAuthority.SelectedValue.Equals("2"))
  277.                 {
  278.                     if (DepartmentIds.Value.Equals(""))
  279.                     {
  280.                         string[] departments = DepartmentIds.Value.Split(new char[] { ',' });
  281.                         for (int i = 0; i < departments.Length; i++)
  282.                         {
  283.                             cmd = new SqlCommand("insert into OA_ANNOUNCEMENT_DEPARTMENT(a_id, department_id)" +
  284.                                 " values (" + Session["id"] + ", " + departments[i] + ")", conn);
  285.                             cmd.ExecuteNonQuery();
  286.                         }
  287.                     }
  288.                     else
  289.                     {
  290.                         string[] departments = DepartmentIds.Value.Split(new char[] { ',' });
  291.                         cmd = new SqlCommand("delete from OA_ANNOUNCEMENT_DEPARTMENT where a_id=" + Session["id"], conn);
  292.                         cmd.ExecuteNonQuery();
  293.                         for (int i = 0; i < departments.Length; i++)
  294.                         {
  295.                             cmd = new SqlCommand("insert into OA_ANNOUNCEMENT_DEPARTMENT(a_id, department_id)" +
  296.                                 " values (" + Session["id"] + ", " + departments[i] + ")", conn);
  297.                             cmd.ExecuteNonQuery();
  298.                         }
  299.                     }
  300.                 }
  301.                 conn.Close();
  302.                 Response.Write("<script> alert('修改成功')</script>");
  303.                 TxtSubject.Text = "";
  304.                 TxtContent.Text = "";
  305.                 DepartmentIds.Value = "";
  306.     }
  307.     //       
  308.     Response.Redirect("../announcement/announce_mgr_list.aspx");
  309. }
  310.     protected void BtnDelete_Click(object sender, EventArgs e)
  311.     {
  312.         LbAttachment.Visible = false;
  313.         BtnDelete.Visible = false;
  314.         FileUpload1.Visible = true;
  315.     }
  316.     protected void RbAuthority_Change(object sender, EventArgs e)
  317.     {
  318.         if (RbAuthority.SelectedValue.Equals("2"))
  319.         {
  320.             BtnSelect.Visible = true;
  321.             LblDepartments.Visible = true;
  322.         }
  323.         else
  324.         {
  325.             BtnSelect.Visible = false;
  326.             LblDepartments.Visible = false;
  327.         }
  328.     }
  329. }