ShortcutNewMessage.aspx.cs
资源名称:OA_at.rar [点击查看]
上传用户:tree100901
上传日期:2007-06-03
资源大小:2295k
文件大小:8k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Web;
- using System.IO;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace OA
- {
- /// <summary>
- /// ShortcutNewMessage 的摘要说明。
- /// </summary>
- public class ShortcutNewMessage : System.Web.UI.Page
- {
- protected System.Data.SqlClient.SqlConnection MyConnection;
- protected System.Web.UI.WebControls.TextBox Accepter;
- protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
- protected System.Web.UI.HtmlControls.HtmlInputFile Attachment;
- protected System.Web.UI.HtmlControls.HtmlInputButton Send;
- protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
- protected FreeTextBoxControls.FreeTextBox Contents;
- protected System.Data.SqlClient.SqlCommand MyCommand;
- private void Page_Load(object sender, System.EventArgs e)
- {
- if(Request.QueryString["Accepter"]!=null)
- Accepter.Text = Request.QueryString["Accepter"].ToString();
- // 在此处放置用户代码以初始化页面
- }
- private void Send_ServerClick(object sender, System.EventArgs e)
- {
- string filename = "";
- string filenamen = "";
- MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
- SqlCommand MyCommand0 = new SqlCommand("PersonID",MyConnection);
- MyCommand0.CommandType = CommandType.StoredProcedure;
- MyCommand0.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50));
- MyCommand0.Parameters["@Name"].Value = User.Identity.Name;
- MyConnection.Open();
- SqlDataReader myReader = MyCommand0.ExecuteReader(CommandBehavior.CloseConnection);
- string PID="";
- try
- {
- while(myReader.Read())
- {
- PID = myReader.GetInt32(0).ToString();
- }
- myReader.Close();
- }
- finally
- {
- }
- string[] myData = Accepter.Text.Trim().Replace(",,",",").Split(new Char [] {','});
- string AccepterSec = "";
- string Sumac ="";
- string [] str = RemoveDup(myData);
- for(int i = 0; i<str.Length; i++)
- {
- SqlCommand MyCommand1 = new SqlCommand("PersonID",MyConnection);
- MyCommand1.CommandType = CommandType.StoredProcedure;
- MyCommand1.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50));
- MyCommand1.Parameters["@Name"].Value = str[i];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader1 = MyCommand1.ExecuteReader();
- while(myReader1.Read())
- {
- AccepterSec = myReader1.GetInt32(0).ToString();
- }
- myReader1.Close();
- if(AccepterSec.Trim()!="")
- {
- Sumac += ","+AccepterSec;
- }
- }
- if(Sumac!="")
- {
- MyCommand=new SqlCommand("InsertMessage",MyConnection);
- MyCommand.CommandType = CommandType.StoredProcedure;
- if (Attachment.PostedFile != null)
- filename=Path.GetFileName(Attachment.PostedFile.FileName).Trim();
- int j = 0;
- filenamen=filename;
- if(File.Exists(Server.MapPath("MessageAttachment\"+filenamen)))
- {
- filenamen = filename.Replace(".","("+j.ToString()+").");
- if(File.Exists(Server.MapPath("MessageAttachment\"+filenamen)))
- {
- int lastLocation = filename.LastIndexOf( "." );
- string[] dirs = Directory.GetFiles(Server.MapPath("MessageAttachment\"), ""+filename.Substring(0,lastLocation)+"(*");
- for(int l = 0; l<dirs.Length+1; l++)
- {
- string strf = filenamen.Replace("("+j.ToString()+").","("+l.ToString()+").");
- if(!File.Exists(Server.MapPath("MessageAttachment\"+strf)))
- filenamen = strf;
- }
- }
- }
- if(filenamen.Trim()!="")
- Attachment.PostedFile.SaveAs(Server.MapPath("MessageAttachment\"+filenamen));
- MyCommand.Parameters.Add(new SqlParameter("@Attachment", SqlDbType.NVarChar, 50));
- MyCommand.Parameters["@Attachment"].Value = filenamen;
- MyCommand.Parameters.Add(new SqlParameter("@Contents",SqlDbType.NVarChar, 1000));
- MyCommand.Parameters["@Contents"].Value = Contents.Text;
- MyCommand.Parameters.Add(new SqlParameter("@PersonID",SqlDbType.Int, 4));
- MyCommand.Parameters["@PersonID"].Value = PID;
- MyCommand.Parameters.Add(new SqlParameter("@AccepterID", SqlDbType.NVarChar, 120));
- MyCommand.Parameters["@AccepterID"].Value = "("+Sumac.Substring(1).Replace(",", "),(")+")";
- MyCommand.Parameters.Add(new SqlParameter("@State",SqlDbType.Int, 4));
- MyCommand.Parameters["@State"].Value = "0";
- try
- {
- MyCommand.ExecuteNonQuery();
- }
- catch
- {
- }
- MyConnection.Close();
- }
- for(int i = 0; i<str.Length; i++)
- {
- SqlCommand MyCommand1 = new SqlCommand("PersonID",MyConnection);
- MyCommand1.CommandType = CommandType.StoredProcedure;
- MyCommand1.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50));
- MyCommand1.Parameters["@Name"].Value = str[i];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader1 = MyCommand1.ExecuteReader();
- while(myReader1.Read())
- {
- AccepterSec = myReader1.GetInt32(0).ToString();
- }
- myReader1.Close();
- if(AccepterSec.Trim()!="")
- {
- MyCommand=new SqlCommand("InsertMessage",MyConnection);
- MyCommand.CommandType = CommandType.StoredProcedure;
- int j = 0;
- if(File.Exists(Server.MapPath("MessageAttachment\"+filenamen)))
- {
- filenamen = filename.Replace(".","("+j.ToString()+").");
- if(File.Exists(Server.MapPath("MessageAttachment\"+filenamen)))
- {
- int lastLocation = filename.LastIndexOf( "." );
- string[] dirs = Directory.GetFiles(Server.MapPath("MessageAttachment\"), ""+filename.Substring(0,lastLocation)+"(*");
- for(int l = 0; l<dirs.Length+1; l++)
- {
- string strf = filenamen.Replace("("+j.ToString()+").","("+l.ToString()+").");
- if(!File.Exists(Server.MapPath("MessageAttachment\"+strf)))
- filenamen = strf;
- }
- }
- }
- if(filenamen.Trim()!="")
- Attachment.PostedFile.SaveAs(Server.MapPath("MessageAttachment\"+filenamen));
- MyCommand.Parameters.Add(new SqlParameter("@Attachment", SqlDbType.NVarChar, 50));
- MyCommand.Parameters["@Attachment"].Value = filenamen;
- MyCommand.Parameters.Add(new SqlParameter("@Contents",SqlDbType.NVarChar, 1000));
- MyCommand.Parameters["@Contents"].Value = Contents.Text;
- MyCommand.Parameters.Add(new SqlParameter("@PersonID",SqlDbType.Int, 4));
- MyCommand.Parameters["@PersonID"].Value = PID;
- MyCommand.Parameters.Add(new SqlParameter("@AccepterID", SqlDbType.NVarChar, 120));
- MyCommand.Parameters["@AccepterID"].Value = AccepterSec;
- MyCommand.Parameters.Add(new SqlParameter("@State",SqlDbType.Int, 4));
- MyCommand.Parameters["@State"].Value = "0";
- try
- {
- MyCommand.ExecuteNonQuery();
- RegisterStartupScript("alert","<script>alert('给"+str[i]+"的信息发送成功!');window.close();</script>");
- }
- catch
- {
- RegisterStartupScript("alert","<script>alert('你填写的对象"+str[i]+"不存在!')</script>");
- }
- }
- }
- Accepter.Text="";
- Contents.Text="";
- }
- public static string[] RemoveDup(string[] myData)
- {
- if (myData.Length > 0)
- {
- Array.Sort(myData);
- int size = 1;
- for (int i=1; i<myData.Length; i++)
- if (myData[i] != myData[i-1])
- size++;
- string[] myTempData = new string[size];
- int j=0;
- myTempData[j++] = myData[0];
- for (int i=1; i<myData.Length; i++)
- if (myData[i] != myData[i-1])
- myTempData[j++] = myData[i];
- return myTempData;
- }
- return myData;
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Send.ServerClick += new System.EventHandler(this.Send_ServerClick);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- }
- }