MyReceiveMessage.aspx.cs
上传用户:tree100901
上传日期:2007-06-03
资源大小:2295k
文件大小:9k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Web;
  9. using System.Web.SessionState;
  10. using System.Web.UI;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.HtmlControls;
  13. namespace OA
  14. {
  15. /// <summary>
  16. /// MyReceiveMessage 的摘要说明。
  17. /// </summary>
  18. public class MyReceiveMessage : System.Web.UI.Page
  19. {
  20. protected System.Data.SqlClient.SqlConnection MyConnection;
  21. protected System.Web.UI.WebControls.Label stats;
  22. protected System.Web.UI.WebControls.DataGrid MyDataGrid;
  23. private int totalMessage;
  24. public string PID;
  25. private void Page_Load(object sender, System.EventArgs e)
  26. {
  27. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  28. SqlCommand MyCommand00 = new SqlCommand("PersonID",MyConnection);
  29. MyCommand00.CommandType = CommandType.StoredProcedure;
  30. MyCommand00.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50));
  31. MyCommand00.Parameters["@Name"].Value = User.Identity.Name;
  32. MyConnection.Open();
  33. SqlDataReader myReader00 =  MyCommand00.ExecuteReader();
  34. myReader00.Read(); 
  35. PID = myReader00.GetInt32(0).ToString();
  36. myReader00.Close();
  37. if(!IsPostBack)
  38. {
  39. MyDataGrid.DataSource = CreateDataSource();
  40. MyDataGrid.DataBind();
  41. }
  42. // 在此处放置用户代码以初始化页面
  43. }
  44. private ICollection CreateDataSource() 
  45. {
  46. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  47. SqlDataAdapter MyCommand = new SqlDataAdapter("SelectReceiveMessage",MyConnection);
  48. MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
  49. MyCommand.SelectCommand.Parameters.Add(new SqlParameter("@AccepterID", SqlDbType.Int, 4));
  50. MyCommand.SelectCommand.Parameters["@AccepterID"].Value = PID;
  51. DataSet ds = new DataSet();
  52. MyCommand.Fill(ds,"Message");
  53. MyConnection.Close();
  54. DataView dv = ds.Tables[0].DefaultView;
  55. totalMessage=ds.Tables[0].Rows.Count;
  56. if (Session["tableReceiveMessage"] == null)
  57. Session["tableReceiveMessage"] = ds.Tables[0];
  58. if (Session["sorterReceiveMessage"] == null)
  59. {
  60. Session["sorterReceiveMessage"] ="Date";
  61. Session["sortReceiveMessage"]=" DESC";
  62. }
  63. dv.Sort = (String) Session["sorterReceiveMessage"]+(String) Session["sortReceiveMessage"];
  64. ShowStats();
  65. return dv;
  66. }
  67. public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) 
  68. {
  69. DataGrid gridThatCausedEvent = (DataGrid) sender;
  70. gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
  71. gridThatCausedEvent.DataSource = CreateDataSource();
  72. gridThatCausedEvent.DataBind();
  73. }
  74. public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e) 
  75. {
  76. Session["sorterReceiveMessage"] = (string)e.SortExpression;
  77. Session["sortReceiveMessage"]=(Session["sortReceiveMessage"].ToString()==" ASC")?
  78. " DESC":" ASC";
  79. MyDataGrid.DataSource = CreateDataSource();
  80. MyDataGrid.DataBind();
  81. foreach (DataGridItem item in MyDataGrid.Controls[0].Controls)
  82. {
  83. if (item.ItemType == ListItemType.Header)
  84. {
  85. Label lb =(Label)item.FindControl((string)Session["sorterReceiveMessage"]);
  86. lb.Text = (Session["sortReceiveMessage"].ToString()==" DESC")?
  87. "↓":"↑";
  88. }
  89. }
  90. }
  91. private void ShowStats() 
  92. {
  93. if (totalMessage == 0) 
  94. {
  95. MyDataGrid.Visible = false;
  96. stats.Text = "未找到符合条件的记录!";
  97. }
  98. else 
  99. {
  100. MyDataGrid.Visible = true;
  101. int startOffset = (totalMessage > 0) ? 
  102. (MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize+1) : 0;
  103. int pageEndOffset = (MyDataGrid.CurrentPageIndex+1)*(MyDataGrid.PageSize);
  104. int endOffset = (pageEndOffset > totalMessage) ? totalMessage : pageEndOffset;
  105. string SortName;
  106. switch(Session["sorterReceiveMessage"].ToString())
  107. {
  108. case "Contents":
  109. SortName = "信息内容";
  110. break;
  111. case "Date":
  112. SortName="发送时间";
  113. break;
  114. case "PersonID":
  115. SortName = "发送人";
  116. break;
  117. case "ReadTime":
  118. SortName = "阅读时间";
  119. break;
  120. default:
  121. SortName="发送时间";
  122. break;
  123. }
  124. stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录  根据</B>{3}<B>排序</B>",
  125. startOffset,endOffset,totalMessage,SortName);
  126. MyDataGrid.PagerStyle.Visible = (totalMessage <= MyDataGrid.PageSize) ? false : true;
  127. }
  128. }
  129. public void MyDataGrid_ItemCreated(Object sender, DataGridItemEventArgs e) 
  130. {
  131. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  132. {
  133. }
  134. if((e.Item.ItemType == ListItemType.Header))
  135. {
  136. for(int i = 0; i<4; i++)
  137. {
  138. Label lb = new Label();
  139. if(i==0)
  140. lb.ID = "Contents";
  141. if(i==1)
  142. lb.ID = "Date";
  143. if(i==2)
  144. lb.ID = "PersonID";
  145. if(i==3)
  146. lb.ID = "ReadTime";
  147. lb.Text = "↓";
  148. e.Item.Cells[i].Controls.Add(lb);
  149. }
  150. }         
  151. }
  152. public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e) 
  153. {
  154. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  155. {
  156. string MessageID = DataBinder.Eval(e.Item.DataItem, "MessageID").ToString();
  157. string AID =e.Item.Cells[2].Text.Trim();
  158. SqlCommand MyCommand0 = new SqlCommand("PersonName",MyConnection);
  159. MyCommand0.CommandType = CommandType.StoredProcedure;
  160. MyCommand0.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
  161. MyCommand0.Parameters["@PersonID"].Value = AID;
  162. if(MyConnection.State.ToString()=="Closed")
  163. MyConnection.Open(); 
  164. SqlDataReader myReader0 =  MyCommand0.ExecuteReader();
  165. while(myReader0.Read())
  166. e.Item.Cells[2].Text = myReader0["Name"].ToString();
  167. myReader0.Close();
  168. MyConnection.Close(); 
  169. string Contents = DataBinder.Eval(e.Item.DataItem, "Contents").ToString();
  170. string Cont = System.Text.RegularExpressions.Regex.Replace(Contents,"<[^>]+>","");
  171. if(Cont.Length>15)
  172. Cont = Cont.Substring(0,15)+"......";
  173. 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,width=423,height=303');window.close();>"+Cont+"</a>";
  174. if(e.Item.Cells[4].Text.Trim()!=null)
  175. 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>";
  176. LinkButton myDeleteButton;
  177. myDeleteButton = (LinkButton) e.Item.Cells[5].Controls[0];
  178. myDeleteButton.Attributes.Add("onclick", @"return confirm('你确认要删除此信息吗?');");
  179. }
  180. }
  181. public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E) 
  182. {
  183. string MA="";
  184. SqlCommand MyCommand0 = new SqlCommand("SelectMessageAttachment",MyConnection);
  185. MyCommand0.CommandType = CommandType.StoredProcedure;
  186. MyCommand0.Parameters.Add(new SqlParameter("@MessageID", SqlDbType.Int, 4));
  187. MyCommand0.Parameters["@MessageID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
  188. if(MyConnection.State.ToString()=="Closed")
  189. MyConnection.Open(); 
  190. SqlDataReader myReader0 =  MyCommand0.ExecuteReader();
  191. while(myReader0.Read())
  192. MA= myReader0["Attachment"].ToString();
  193. myReader0.Close();
  194. MyConnection.Close(); 
  195. if((MA.Trim()!="")&&(File.Exists(Server.MapPath("MessageAttachment\"+MA))))
  196. File.Delete(Server.MapPath("MessageAttachment\"+MA));
  197. SqlCommand MyCommand = new SqlCommand("DeleteSenderMessage",MyConnection);
  198. MyCommand.CommandType = CommandType.StoredProcedure;
  199. MyCommand.Parameters.Add(new SqlParameter("@MessageID", SqlDbType.Int, 4));
  200. MyCommand.Parameters["@MessageID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
  201. if(MyConnection.State.ToString()=="Closed")
  202. MyConnection.Open(); 
  203. try
  204. {
  205. MyCommand.ExecuteNonQuery();
  206. }
  207. catch (SqlException)
  208. {
  209. RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
  210. }
  211. MyConnection.Close();
  212. if ((MyDataGrid.CurrentPageIndex!=0)&&((int)E.Item.ItemIndex==0))
  213. {
  214. if (MyDataGrid.Items.Count==1)
  215. MyDataGrid.CurrentPageIndex-=1;
  216. }
  217. MyDataGrid.DataSource = CreateDataSource();
  218. MyDataGrid.DataBind();
  219. Response.Write("<script>opener.location.href=opener.location.href;opener=null;window.close();</"+"script>");
  220. }
  221. #region Web 窗体设计器生成的代码
  222. override protected void OnInit(EventArgs e)
  223. {
  224. //
  225. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  226. //
  227. InitializeComponent();
  228. base.OnInit(e);
  229. }
  230. /// <summary>
  231. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  232. /// 此方法的内容。
  233. /// </summary>
  234. private void InitializeComponent()
  235. {    
  236. this.Load += new System.EventHandler(this.Page_Load);
  237. }
  238. #endregion
  239. }
  240. }