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

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_add : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     protected void BtnOk_Click(object sender, EventArgs e)
  18.     {
  19.         if (this.CheckValidate())
  20.         {
  21.             DateMgr mgr = new DateMgr();
  22.             string time = mgr.getTime();
  23.             string user = "";
  24.             if (Session["user"] != null)
  25.             {
  26.                 user = ((User)Session["user"]).Emp_name;
  27.             }
  28.             if (this.uploadFile(time))
  29.             {
  30.                 string file_name = "";
  31.                 string save_name = "";
  32.                 if (FileUpload1.HasFile)
  33.                 {
  34.                     file_name = FileUpload1.FileName;
  35.                     save_name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); 
  36.                 }
  37.                 SqlConnection conn = dbConnection.getConnection();
  38.                 conn.Open();
  39.                 int id = 1;
  40.                 SqlCommand cmd = new SqlCommand("select max(id) from OA_ANNOUNCEMENT", conn);
  41.                 try
  42.                 {
  43.                     id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  44.                 }
  45.                 catch { }
  46.                 
  47.                 cmd = new SqlCommand("insert into OA_ANNOUNCEMENT(id, type_id, subject, content, authority, "+
  48.                     "validate_date, attachment, attachment_save_name, attachment_length, is_del, cre_user, cre_date)"+
  49.                     "values("+id+",'"+ DdlType.SelectedValue+"','"+TxtSubject.Text+"','"+
  50.                     TxtContent.Text.Replace("n","<br>").Replace(" ","&nbsp;")+"','"+
  51.                     RbAuthority.SelectedValue+"','"+TxtValidateDate.Text+"','"+file_name+"','"+save_name+"','"+
  52.                     ContentLength.Value+"','N','"+ user+"','"+mgr.getDateTime()+"')", conn);
  53.                 cmd.ExecuteNonQuery();
  54.                 if (RbAuthority.SelectedValue.Equals("2"))
  55.                 {
  56.                     if (!DepartmentIds.Value.Equals(""))
  57.                     { 
  58.                         string [] departments = DepartmentIds.Value.Split(new char[]{','});
  59.                         for (int i = 0; i < departments.Length; i++)
  60.                         {
  61.                             cmd = new SqlCommand("insert into OA_ANNOUNCEMENT_DEPARTMENT(a_id, department_id)"+
  62.                                 " values ("+id+", "+departments[i]+")", conn);
  63.                             cmd.ExecuteNonQuery();
  64.                         }
  65.                     }
  66.                 }
  67.                 conn.Close();
  68.                 Response.Write("<script> alert('发布成功')</script>");
  69.                 TxtSubject.Text = "";
  70.                 TxtContent.Text = "";
  71.                 DepartmentIds.Value = "";
  72.             }
  73.         }
  74.     }
  75.     protected bool CheckValidate()
  76.     {
  77.         if (TxtSubject.Text.Trim().Equals(""))
  78.         {
  79.             Response.Write("<script language='javascript'>alert('主旨不能為空!');</script>");
  80.             return false;
  81.         }
  82.         if (TxtContent.Text.Trim().Equals(""))
  83.         {
  84.             Response.Write("<script language='javascript'>alert('內容不能為空!');</script>");
  85.             return false;
  86.         }
  87.         if (RbAuthority.SelectedValue.Equals("2"))
  88.         {
  89.             if (DepartmentIds.Value.Equals(""))
  90.             {
  91.                 Response.Write("<script language='javascript'>alert('請選擇部門!');</script>");
  92.                 return false;
  93.             }
  94.         }
  95.         return true;
  96.     }
  97.     protected void RbAuthority_Change(object sender, EventArgs e)
  98.     {
  99.         if (RbAuthority.SelectedValue.Equals("2"))
  100.         {
  101.             BtnSelect.Visible = true;
  102.             LblDepartments.Visible = true;
  103.         }
  104.         else
  105.         {
  106.             BtnSelect.Visible = false;
  107.             LblDepartments.Visible = false;
  108.         }
  109.     }
  110.     protected bool uploadFile(string time)
  111.     {
  112.         bool flag = false;
  113.         string directory = "d:\oa(new)\web\oa_upload\announce\";
  114.         if (FileUpload1.HasFile)
  115.         {
  116.             bool extenstion = false;
  117.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  118.             string[] allowedExtensions = 
  119.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  120.             for (int i = 0; i < allowedExtensions.Length; i++)
  121.             {
  122.                 if (fileExtension == allowedExtensions[i])
  123.                 {
  124.                     extenstion = true;
  125.                     break;
  126.                 }
  127.             }
  128.             if (!extenstion)
  129.             {
  130.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案!');</script>");
  131.                 return false;
  132.             }
  133.             else
  134.             {
  135.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  136.                 FileUpload1.PostedFile.SaveAs(directory + name);
  137.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  138.                 flag = true;
  139.             }
  140.         }
  141.         else
  142.         {
  143.             flag = true;
  144.         }
  145.         return flag;
  146.     }
  147.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  148.     {
  149.         string year = Calendar1.SelectedDate.Year.ToString();
  150.         string month = Calendar1.SelectedDate.Month.ToString();
  151.         if (month.Length == 1)
  152.         {
  153.             month = "0" + month;
  154.         }
  155.         string day = Calendar1.SelectedDate.Day.ToString();
  156.         if (day.Length == 1)
  157.         {
  158.             day = "0" + day;
  159.         }
  160.         TxtValidateDate.Text = year + "-" + month + "-"+ day;
  161.         Calendar1.Visible = false;
  162.     }
  163.     protected void ImageButton1_Click(object sender, EventArgs e)
  164.     {
  165.         if (Calendar1.Visible == true)
  166.         {
  167.             Calendar1.Visible = false;
  168.         }
  169.         else
  170.         {
  171.             Calendar1.Visible = true;
  172.         }
  173.     }
  174. }