ManageMessage.aspx.cs
资源名称:OA_at.rar [点击查看]
上传用户:tree100901
上传日期:2007-06-03
资源大小:2295k
文件大小:10k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.IO;
- using System.Data.SqlClient;
- using System.Data;
- using System.Drawing;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace OA
- {
- /// <summary>
- /// ManageMessage 的摘要说明。
- /// </summary>
- public class ManageMessage : System.Web.UI.Page
- {
- protected System.Data.SqlClient.SqlConnection MyConnection;
- protected System.Web.UI.WebControls.DataGrid MyDataGrid;
- private int totalMessage;
- protected System.Web.UI.WebControls.Label stats;
- public string PID;
- private void Page_Load(object sender, System.EventArgs e)
- {
- CheckUser CHU = new CheckUser();
- CHU.UserName = User.Identity.Name;
- PID = CHU.UserID.ToString();
- if(CHU.Is_FileManager() == false)
- Response.Write("<script>alert('对不起,你没有查看此页面的权限!');history.back(2)</" + "script>");
- MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
- if(!IsPostBack)
- {
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- }
- }
- private ICollection CreateDataSource()
- {
- MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
- SqlDataAdapter MyCommand = new SqlDataAdapter("SelectAllMessage",MyConnection);
- MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
- DataSet ds = new DataSet();
- MyCommand.Fill(ds,"Message");
- MyConnection.Close();
- DataView dv = ds.Tables[0].DefaultView;
- totalMessage=ds.Tables[0].Rows.Count;
- if (Session["tableAllMessage"] == null)
- Session["tableAllMessage"] = ds.Tables[0];
- if (Session["sorterAllMessage"] == null)
- {
- Session["sorterAllMessage"] ="Date";
- Session["sortAllMessage"]=" DESC";
- }
- dv.Sort = (String) Session["sorterAllMessage"]+(String) Session["sortAllMessage"];
- ShowStats();
- return dv;
- }
- public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
- {
- DataGrid gridThatCausedEvent = (DataGrid) sender;
- gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
- gridThatCausedEvent.DataSource = CreateDataSource();
- gridThatCausedEvent.DataBind();
- }
- public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
- {
- Session["sorterAllMessage"] = (string)e.SortExpression;
- Session["sortAllMessage"]=(Session["sortAllMessage"].ToString()==" ASC")?
- " DESC":" ASC";
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- foreach (DataGridItem item in MyDataGrid.Controls[0].Controls)
- {
- if (item.ItemType == ListItemType.Header)
- {
- Label lb =(Label)item.FindControl((string)Session["sorterAllMessage"]);
- lb.Text = (Session["sortAllMessage"].ToString()==" DESC")?
- "↓":"↑";
- }
- }
- }
- private void ShowStats()
- {
- if (totalMessage == 0)
- {
- MyDataGrid.Visible = false;
- stats.Text = "未找到符合条件的记录!";
- }
- else
- {
- MyDataGrid.Visible = true;
- int startOffset = (totalMessage > 0) ?
- (MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize+1) : 0;
- int pageEndOffset = (MyDataGrid.CurrentPageIndex+1)*(MyDataGrid.PageSize);
- int endOffset = (pageEndOffset > totalMessage) ? totalMessage : pageEndOffset;
- string SortName;
- switch(Session["sorterAllMessage"].ToString())
- {
- case "Contents":
- SortName = "信息内容";
- break;
- case "Date":
- SortName="发送时间";
- break;
- case "AccepterID":
- SortName = "接收人";
- break;
- case "PersonID":
- SortName = "发送人";
- break;
- default:
- SortName="发送时间";
- break;
- }
- stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录 根据</B>{3}<B>排序</B>",
- startOffset,endOffset,totalMessage,SortName);
- MyDataGrid.PagerStyle.Visible = (totalMessage <= MyDataGrid.PageSize) ? false : true;
- }
- }
- public void MyDataGrid_ItemCreated(Object sender, DataGridItemEventArgs e)
- {
- if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
- {
- }
- if((e.Item.ItemType == ListItemType.Header))
- {
- for(int i = 0; i<4; i++)
- {
- Label lb = new Label();
- if(i==0)
- lb.ID = "Contents";
- if(i==1)
- lb.ID = "Date";
- if(i==2)
- lb.ID = "AccepterID";
- if(i==3)
- lb.ID = "PersonID";
- lb.Text = "↓";
- e.Item.Cells[i].Controls.Add(lb);
- }
- }
- }
- public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e)
- {
- if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
- {
- string MessageID = DataBinder.Eval(e.Item.DataItem, "MessageID").ToString();
- string AID =e.Item.Cells[2].Text.Trim().Replace("(","").Replace(")","");
- string [] str = AID.Split(new char[] {','});
- string Accepter="";
- for(int i=0; i<str.Length; i++)
- {
- SqlCommand MyCommand0 = new SqlCommand("PersonName",MyConnection);
- MyCommand0.CommandType = CommandType.StoredProcedure;
- MyCommand0.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand0.Parameters["@PersonID"].Value = str[i];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader0 = MyCommand0.ExecuteReader();
- while(myReader0.Read())
- {
- Accepter += ","+myReader0["Name"].ToString().Trim();
- }
- myReader0.Close();
- MyConnection.Close();
- }
- e.Item.Cells[2].ToolTip = Accepter.Substring(1);
- e.Item.Cells[2].Style["cursor"] = "hand";
- if(Accepter.Length>11)
- Accepter = Accepter.Substring(1,10)+"......";
- else
- Accepter = Accepter.Substring(1);
- e.Item.Cells[2].Text = Accepter;
- string Contents = DataBinder.Eval(e.Item.DataItem, "Contents").ToString();
- string Cont = System.Text.RegularExpressions.Regex.Replace(Contents,"<[^>]+>","");
- if(Cont.Length>15)
- Cont = Cont.Substring(0,15)+"......";
- e.Item.Cells[0].Text = "<a href = javascript:window.open('MessageDetails.aspx?MessageID="+MessageID+"','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0,height=303');window.close();>"+Cont+"</a>";
- SqlCommand MyCommand1 = new SqlCommand("PersonName",MyConnection);
- MyCommand1.CommandType = CommandType.StoredProcedure;
- MyCommand1.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand1.Parameters["@PersonID"].Value = e.Item.Cells[3].Text;
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader1 = MyCommand1.ExecuteReader();
- myReader1.Read();
- e.Item.Cells[3].Text = myReader1["Name"].ToString();
- myReader1.Close();
- MyConnection.Close();
- if(e.Item.Cells[4].Text.Trim()!=null)
- e.Item.Cells[4].Text="<a href=javascript:window.open('MessageAttachment\\"+e.Item.Cells[4].Text+"','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0');window.close();>"+e.Item.Cells[4].Text+"</a>";
- LinkButton myDeleteButton1;
- LinkButton myDeleteButton2;
- myDeleteButton1 = (LinkButton) e.Item.Cells[5].Controls[0];
- myDeleteButton1.Attributes.Add("onclick", @"window.open('ViewMessageState.aspx?MessageID="+MessageID+"','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=188,height="+str.Length*35+"');return false;");
- myDeleteButton2 = (LinkButton) e.Item.Cells[6].Controls[0];
- myDeleteButton2.Attributes.Add("onclick", @"return confirm('你确认要删除此信息吗?');");
- }
- }
- public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E)
- {
- string MA="";
- SqlCommand MyCommand0 = new SqlCommand("SelectMessageAttachment",MyConnection);
- MyCommand0.CommandType = CommandType.StoredProcedure;
- MyCommand0.Parameters.Add(new SqlParameter("@MessageID", SqlDbType.Int, 4));
- MyCommand0.Parameters["@MessageID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader0 = MyCommand0.ExecuteReader();
- while(myReader0.Read())
- {
- MA = myReader0["Attachment"].ToString();
- }
- myReader0.Close();
- MyConnection.Close();
- if((MA.Trim()!="")&&(File.Exists(Server.MapPath("MessageAttachment\"+MA))))
- File.Delete(Server.MapPath("MessageAttachment\"+MA));
- SqlCommand MyCommand = new SqlCommand("DeleteSenderMessage",MyConnection);
- MyCommand.CommandType = CommandType.StoredProcedure;
- MyCommand.Parameters.Add(new SqlParameter("@MessageID", SqlDbType.Int, 4));
- MyCommand.Parameters["@MessageID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- try
- {
- MyCommand.ExecuteNonQuery();
- }
- catch (SqlException)
- {
- RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
- }
- MyConnection.Close();
- if ((MyDataGrid.CurrentPageIndex!=0)&&((int)E.Item.ItemIndex==0))
- {
- if (MyDataGrid.Items.Count==1)
- MyDataGrid.CurrentPageIndex-=1;
- }
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- Response.Write("<script>opener.location.href=opener.location.href;opener=null;window.close();</"+"script>");
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- }
- }