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

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. using System.IO;
  13. public partial class web_mail_mail_list_send : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         LoginDAO.CheckLogin(Session, Response, "../", 2);
  18.         //設置主鍵
  19.         MailList.DataKeyNames = new string[] { "id" };
  20.         if (!IsPostBack)
  21.         {
  22.             if (Request.QueryString["s_id"] != null)
  23.             {
  24.                 PnlMail.Visible = true;
  25.                 this.setMail(Request.QueryString["s_id"]);
  26.             }
  27.         }
  28.     }
  29.     protected void setMail(string s_id)
  30.     {
  31.         try
  32.         {
  33.             SqlConnection conn = dbConnection.getConnection();
  34.             conn.Open();
  35.             SqlCommand cmd = new SqlCommand("SELECT s.mail_id, m.title, m.content, s.send_date, "+
  36.                 "e.emp_no, e.emp_name, p.name position FROM dbo.OA_MAIL_SENDER AS s INNER JOIN "+
  37.                 "dbo.OA_EMPLOYEE AS e ON s.sender = e.id INNER JOIN dbo.OA_MAIL_CONTENT AS m ON "+
  38.                 "s.mail_id = m.id inner join OA_EMPLOYEE_POSITION p on e.position=p.id WHERE s.id=" + 
  39.                 s_id, conn);
  40.             SqlDataReader dr = cmd.ExecuteReader();
  41.             string mail_id = "0";
  42.             if (dr.Read())
  43.             {
  44.                 mail_id = dr["mail_id"].ToString();
  45.                 LblTitle.Text = dr["title"].ToString();
  46.                 TxtContent.Text = dr["content"].ToString().Replace("<br>", "n").Replace("&nbsp;", " ");
  47.                 LblSendDate.Text = dr["send_date"].ToString();
  48.                 LblSender.Text = dr["emp_no"].ToString() + " " + dr["emp_name"].ToString() + dr["position"].ToString();
  49.             }
  50.             dr.Close();
  51.             BtnStatistics.OnClientClick = "return openStatistics('"+mail_id+"')";
  52.             cmd = new SqlCommand("select  e.emp_no, e.emp_name, p.name from OA_MAIL_RECEIVER r inner join " +
  53.                 "OA_EMPLOYEE e on r.receiver=e.id inner join OA_EMPLOYEE_POSITION p on e.position=p.id " +
  54.                 "where mail_id=" + mail_id, conn);
  55.             dr = cmd.ExecuteReader();
  56.             while (dr.Read())
  57.             {
  58.                 LblReceivers.Text = LblReceivers.Text + dr["emp_no"].ToString() + " " +
  59.                     dr["emp_name"].ToString() + dr["name"].ToString() + "; ";
  60.             }
  61.             dr.Close();
  62.             if (!LblReceivers.Text.Equals(""))
  63.             {
  64.                 LblReceivers.Text = LblReceivers.Text.Substring(0, LblReceivers.Text.Length - 2);
  65.             }
  66.             cmd = new SqlCommand("select cn_name, en_name, content_length from OA_MAIL_ATTACHMENT where mail_id=" + mail_id, conn);
  67.             dr = cmd.ExecuteReader();
  68.             if (dr.Read())
  69.             {
  70.                 BtnAttachment.Text = dr["cn_name"].ToString();
  71.                 AttachmentEnName.Value = dr["en_name"].ToString();
  72.                 ContentLength.Value = dr["content_length"].ToString();
  73.             }
  74.             else
  75.             {
  76.                 //沒有附件
  77.                 LblNoAttachment.Visible = true;
  78.                 BtnAttachment.Visible = false;
  79.             }
  80.             dr.Close();
  81.             cmd = new SqlCommand("select count(id) from OA_MAIL_RECEIVER where mail_id=" + mail_id +
  82.                 " and is_read=1", conn);
  83.             BtnStatistics.Text = Convert.ToInt32(cmd.ExecuteScalar()) + "人已讀;";
  84.             cmd = new SqlCommand("select count(id) from OA_MAIL_RECEIVER where mail_id=" + mail_id +
  85.                 " and is_read=0", conn);
  86.             BtnStatistics.Text = BtnStatistics.Text + Convert.ToInt32(cmd.ExecuteScalar()) + "人未讀";
  87.             conn.Close();
  88.         }
  89.         catch (Exception ex)
  90.         {
  91.             Response.Write(ex.ToString());
  92.         }
  93.     }
  94.     protected void DownLoadFile(string name)
  95.     {
  96.         string server_ip = "";
  97.         string root = "";
  98.         string folder = "";
  99.         if (Application["FILE_SERVER_IP"] != null)
  100.         {
  101.             server_ip = Application["FILE_SERVER_IP"].ToString();
  102.         }
  103.         if (Application["FILE_SERVER_ROOT"] != null)
  104.         {
  105.             root = Application["FILE_SERVER_ROOT"].ToString();
  106.         }
  107.         if (Application["MAIL_FOLDER"] != null)
  108.         {
  109.             folder = Application["MAIL_FOLDER"].ToString();
  110.         }
  111.         //string directory = "\\" + server_ip + "\" + root + "\" + folder + "\";
  112.         string directory = "\web\oa_upload\mail\";
  113.         try
  114.         {
  115.             Response.Clear();
  116.             Response.ClearHeaders();
  117.             Response.Buffer = false;
  118.             Response.ContentType = "application/octet-stream";
  119.             Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(BtnAttachment.Text, System.Text.Encoding.UTF8));
  120.             Response.AppendHeader("Content-Length", ContentLength.Value);
  121.             Response.WriteFile(directory + name);
  122.             Response.Flush();
  123.             Response.End();
  124.         }
  125.         catch (Exception ex)
  126.         {
  127.             Response.Write(ex.Message);
  128.         }
  129.     }
  130.     protected void MailList_RowDataBound(object sender, GridViewRowEventArgs e)
  131.     {
  132.         //行高亮
  133.         if (e.Row.RowType == DataControlRowType.DataRow)
  134.         {
  135.             e.Row.Attributes.Add("onmouseover", "this.style.cursor='pointer';");
  136.             e.Row.Attributes.Add("onclick", "location.href='mail_list_send.aspx?s_id=" + MailList.DataKeys[e.Row.RowIndex].Value.ToString() + "'");
  137.         }
  138.     }
  139.     protected void BtnAttachment_Click(object sender, EventArgs e)
  140.     {
  141.         this.DownLoadFile(AttachmentEnName.Value);
  142.     }
  143.     protected void MailList_RowCommand(object sender, GridViewCommandEventArgs e)
  144.     {
  145.         if (e.CommandName.Equals("DeleteData"))
  146.         { 
  147.             string id = e.CommandArgument.ToString();
  148.             //點擊刪除按鈕
  149.             try
  150.             {
  151.                 SqlConnection conn = dbConnection.getConnection();
  152.                 conn.Open();
  153.                 SqlCommand cmd = new SqlCommand("update OA_MAIL_SENDER set is_del=1 where id=" + id, conn);
  154.                 cmd.ExecuteNonQuery();
  155.                 conn.Close();
  156.                 MailList.DataBind();
  157.                 PnlMail.Visible = false;
  158.             }
  159.             catch (Exception ex)
  160.             {
  161.                 Response.Write(ex.Message);
  162.             }
  163.         }
  164.     }
  165.     protected void MailList_RowCreated(object sender, GridViewRowEventArgs e)
  166.     {
  167.         if (e.Row.RowType == DataControlRowType.DataRow)
  168.         {
  169.             LinkButton btnDelete = (LinkButton)e.Row.FindControl("BtnDelete");
  170.             btnDelete.CommandArgument = MailList.DataKeys[e.Row.RowIndex].Value.ToString();
  171.             SqlConnection conn = dbConnection.getConnection();
  172.             conn.Open();
  173.             Label label = (Label)e.Row.FindControl("LblStatistics");
  174.             string mail_id = "0";
  175.             SqlCommand cmd = new SqlCommand("SELECT mail_id FROM dbo.OA_MAIL_SENDER where id=" + MailList.DataKeys[e.Row.RowIndex].Value.ToString(), conn);
  176.             SqlDataReader dr = cmd.ExecuteReader();
  177.             if (dr.Read())
  178.             {
  179.                 mail_id = dr["mail_id"].ToString();
  180.             }
  181.             dr.Close();
  182.             
  183.             cmd = new SqlCommand("select count(id) from OA_MAIL_RECEIVER where mail_id=" + mail_id +
  184.                 " and is_read=1", conn);
  185.             label.Text = Convert.ToInt32(cmd.ExecuteScalar()) + "人已讀;";
  186.             cmd = new SqlCommand("select count(id) from OA_MAIL_RECEIVER where mail_id=" + mail_id +
  187.                 " and is_read=0", conn);
  188.             label.Text = label.Text + Convert.ToInt32(cmd.ExecuteScalar()) + "人未讀";
  189.             label = (Label)e.Row.FindControl("LblReceivers");
  190.             cmd = new SqlCommand("select e.emp_name, e.emp_no, p.name postion from OA_MAIL_RECEIVER r "+
  191.                 "inner join OA_EMPLOYEE e on r.receiver=e.id inner join OA_EMPLOYEE_POSITION p on "+
  192.                 "e.position=p.id where r.mail_id="+mail_id, conn);
  193.             dr = cmd.ExecuteReader();
  194.             int count = 0;
  195.             while (dr.Read())
  196.             {
  197.                 if (count < 2)
  198.                 {
  199.                     count = count + 1;
  200.                     label.Text = label.Text + dr["emp_no"].ToString() +
  201.                         dr["emp_name"].ToString() + " " + dr["postion"].ToString() + "; ";
  202.                 }
  203.                 else
  204.                 {
  205.                     break;
  206.                 }
  207.             }
  208.             if(!label.Text.Equals(""))
  209.             {
  210.                 label.Text = label.Text.Substring(0, label.Text.Length - 2);
  211.                 label.Text = label.Text + "...";
  212.             }
  213.             conn.Close();
  214.         }
  215.     }
  216. }