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 void LbAttachment_Click(object sender, EventArgs e)
  156.     {
  157.         this.DownLoadFile(AttachmentSaveName.Value);
  158.     }
  159.     protected void DownLoadFile(string name)
  160.     {
  161.         string directory = "..\oa_upload\announce\";
  162.         try
  163.         {
  164.             Response.Clear();
  165.             Response.ClearHeaders();
  166.             Response.Buffer = false;
  167.             Response.ContentType = "application/octet-stream";
  168.             Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(LbAttachment.Text, System.Text.Encoding.UTF8));
  169.             Response.AppendHeader("Content-Length", ContentLength.Value);
  170.             Response.WriteFile(directory + name);
  171.             Response.Flush();
  172.             Response.End();
  173.         }
  174.         catch (Exception ex)
  175.         {
  176.             Response.Write(ex.Message);
  177.         }
  178.     }
  179.     protected void AnnounceList_RowDeleted(object sender, GridViewDeletedEventArgs e)
  180.     {
  181.         //刷新樹
  182.         Response.Write("<script language='javascript'>window.parent.frames.location.reload()</script>");
  183.         AnnounceList.DataBind();
  184.     }
  185.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  186.     {
  187.         string year = Calendar1.SelectedDate.Year.ToString();
  188.         string month = Calendar1.SelectedDate.Month.ToString();
  189.         if (month.Length == 1)
  190.         {
  191.             month = "0" + month;
  192.         }
  193.         string day = Calendar1.SelectedDate.Day.ToString();
  194.         if (day.Length == 1)
  195.         {
  196.             day = "0" + day;
  197.         }
  198.         TxtValidateDate.Text = year + "-" + month + "-" + day;
  199.         Calendar1.Visible = false;
  200.     }
  201.     protected void ImageButton1_Click(object sender, EventArgs e)
  202.     {
  203.         if (Calendar1.Visible == true)
  204.         {
  205.             Calendar1.Visible = false;
  206.         }
  207.         else
  208.         {
  209.             Calendar1.Visible = true;
  210.         }
  211.     }
  212.     protected void BtnOk_Click(object sender, EventArgs e)
  213.     {   
  214.         //
  215.         
  216.             DateMgr mgr = new DateMgr();
  217.             string time = mgr.getTime();
  218.             string user = "";
  219.             if (Session["user"] != null)
  220.             {
  221.                 user = ((User)Session["user"]).Emp_name;
  222.             }
  223.             if (this.uploadFile(time))
  224.             {
  225.                 string file_name = "";
  226.                 string save_name = "";
  227.                 if (FileUpload1.HasFile)
  228.                 {
  229.                     file_name = FileUpload1.FileName;
  230.                     save_name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  231.                 }
  232.                 SqlConnection conn = dbConnection.getConnection();
  233.                 conn.Open();            
  234.                              
  235.              SqlCommand   cmd=new SqlCommand("update OA_ANNOUNCEMENT set type_id='" + DdlType.SelectedValue + "', subject='" + TxtSubject.Text +
  236.                 "', content='" + TxtContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp;") + "', authority='" + RbAuthority.SelectedValue +
  237.                 "',validate_date='" + TxtValidateDate.Text + "',attachment='" + file_name + "',attachment_save_name='" + save_name +
  238.                 "',cre_date='" + mgr.getDateTime() + "' where id=" + Session["id"], conn);
  239.                  cmd.ExecuteNonQuery();
  240.                 if (RbAuthority.SelectedValue.Equals("2"))
  241.                 {
  242.                     if (DepartmentIds.Value.Equals(""))
  243.                     {
  244.                         string[] departments = DepartmentIds.Value.Split(new char[] { ',' });
  245.                         for (int i = 0; i < departments.Length; i++)
  246.                         {
  247.                             cmd = new SqlCommand("insert into OA_ANNOUNCEMENT_DEPARTMENT(a_id, department_id)" +
  248.                                 " values (" + Session["id"] + ", " + departments[i] + ")", conn);
  249.                             cmd.ExecuteNonQuery();
  250.                         }
  251.                     }
  252.                     else
  253.                     {
  254.                         string[] departments = DepartmentIds.Value.Split(new char[] { ',' });
  255.                         cmd = new SqlCommand("delete from OA_ANNOUNCEMENT_DEPARTMENT where a_id=" + Session["id"], conn);
  256.                         cmd.ExecuteNonQuery();
  257.                         for (int i = 0; i < departments.Length; i++)
  258.                         {
  259.                             cmd = new SqlCommand("insert into OA_ANNOUNCEMENT_DEPARTMENT(a_id, department_id)" +
  260.                                 " values (" + Session["id"] + ", " + departments[i] + ")", conn);
  261.                             cmd.ExecuteNonQuery();
  262.                         }
  263.                     }
  264.                 }
  265.                 conn.Close();
  266.                 Response.Write("<script> alert('修改成功')</script>");
  267.                 TxtSubject.Text = "";
  268.                 TxtContent.Text = "";
  269.                 DepartmentIds.Value = "";
  270.             
  271.         }
  272.         //       
  273.             Response.Redirect("../announcement/announce_mgr_list.aspx");
  274.       
  275.        
  276.         
  277.     }
  278.     protected bool uploadFile(string time)
  279.     {
  280.         bool flag = false;
  281.         string directory = "d:\oa(new)\web\oa_upload\announce\";
  282.         if (FileUpload1.HasFile)
  283.         {
  284.             bool extenstion = false;
  285.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  286.             string[] allowedExtensions = 
  287.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  288.             for (int i = 0; i < allowedExtensions.Length; i++)
  289.             {
  290.                 if (fileExtension == allowedExtensions[i])
  291.                 {
  292.                     extenstion = true;
  293.                     break;
  294.                 }
  295.             }
  296.             if (!extenstion)
  297.             {
  298.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
  299.                 return false;
  300.             }
  301.             else
  302.             {
  303.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  304.                 FileUpload1.PostedFile.SaveAs(directory + name);
  305.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  306.                 flag = true;
  307.             }
  308.         }
  309.         else
  310.         {
  311.             flag = true;
  312.         }
  313.         return flag;
  314.     }
  315.     protected void BtnDelete_Click(object sender, EventArgs e)
  316.     {
  317.         LbAttachment.Visible = false;
  318.         BtnDelete.Visible = false;
  319.         FileUpload1.Visible = true;
  320.     }
  321.     protected void RbAuthority_Change(object sender, EventArgs e)
  322.     {
  323.         if (RbAuthority.SelectedValue.Equals("2"))
  324.         {
  325.             BtnSelect.Visible = true;
  326.             LblDepartments.Visible = true;
  327.         }
  328.         else
  329.         {
  330.             BtnSelect.Visible = false;
  331.             LblDepartments.Visible = false;
  332.         }
  333.     }
  334. }