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

OA系统

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Web;
  8. using System.Web.SessionState;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.HtmlControls;
  12. namespace OA
  13. {
  14. /// <summary>
  15. /// ManageNotification 的摘要说明。
  16. /// </summary>
  17. public class ManageNotification : System.Web.UI.Page
  18. {
  19. protected System.Data.SqlClient.SqlConnection MyConnection;
  20. protected System.Web.UI.WebControls.DataGrid MyDataGrid;
  21. protected System.Web.UI.WebControls.TextBox Contents;
  22. protected System.Web.UI.HtmlControls.HtmlInputButton AddNotification;
  23. public string PID;
  24. protected System.Web.UI.WebControls.Label stats;
  25. private int totalNotification;
  26. private void Page_Load(object sender, System.EventArgs e)
  27. {
  28. CheckUser CHU = new CheckUser();
  29. CHU.UserName = User.Identity.Name;
  30. PID = CHU.UserID.ToString();
  31. if(CHU.Is_FileManager() == false)
  32. Response.Write("<script>alert('对不起,你没有查看此页面的权限!');history.back(2)</" + "script>");
  33. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  34. if(!IsPostBack)
  35. {
  36. MyDataGrid.DataSource = CreateDataSource();
  37. MyDataGrid.DataBind();
  38. }
  39. // 在此处放置用户代码以初始化页面
  40. }
  41. private ICollection CreateDataSource() 
  42. {
  43. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  44. SqlDataAdapter MyCommand2 = new SqlDataAdapter("SelectAllNotification",MyConnection);
  45. MyCommand2.SelectCommand.CommandType = CommandType.StoredProcedure;
  46. DataSet ds = new DataSet();
  47. MyCommand2.Fill(ds, "Notification");
  48. DataView dv = ds.Tables["Notification"].DefaultView;
  49. totalNotification=ds.Tables["Notification"].Rows.Count;
  50. ShowStats();
  51. return dv;
  52. }
  53. public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) 
  54. {
  55. DataGrid gridThatCausedEvent = (DataGrid) sender;
  56. gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
  57. gridThatCausedEvent.DataSource = CreateDataSource();
  58. gridThatCausedEvent.DataBind();
  59. }
  60. private void AddNotification_ServerClick(object sender, System.EventArgs e)
  61. {
  62. Page.Validate();
  63. if ( !Page.IsValid )
  64. {
  65. return;
  66. }
  67. if(Contents.Text.Trim()!="")
  68. {
  69. SqlCommand MyCommand = new SqlCommand("InsertNotification", MyConnection);
  70. MyCommand.CommandType = CommandType.StoredProcedure;
  71. MyCommand.Parameters.Add(new SqlParameter("@Contents", SqlDbType.NVarChar, 200));
  72. MyCommand.Parameters["@Contents"].Value = Contents.Text;
  73. MyCommand.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
  74. MyCommand.Parameters["@PersonID"].Value = Int32.Parse(PID);
  75. MyCommand.Connection.Open();
  76. try 
  77. {
  78. MyCommand.ExecuteNonQuery();
  79. }
  80. catch 
  81. {
  82. }
  83. MyCommand.Connection.Close();
  84. Contents.Text = "";
  85. MyDataGrid.DataSource = CreateDataSource();
  86. MyDataGrid.DataBind();
  87. }
  88. else
  89. RegisterStartupScript("alert","<script>alert('公告内容不能为空!')</" + "script>");
  90. }
  91. public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E) 
  92. {
  93. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  94. SqlCommand MyCommand = new SqlCommand("DeleteNotification",MyConnection);
  95. MyCommand.CommandType = CommandType.StoredProcedure;
  96. MyCommand.Parameters.Add(new SqlParameter("@NotificationID", SqlDbType.Int, 4));
  97. MyCommand.Parameters["@NotificationID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
  98. if(MyConnection.State.ToString()=="Closed")
  99. MyConnection.Open(); 
  100. try
  101. {
  102. MyCommand.ExecuteNonQuery();
  103. }
  104. catch
  105. {
  106. RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
  107. }
  108. MyConnection.Close();
  109. MyDataGrid.DataSource = CreateDataSource();
  110. MyDataGrid.DataBind();
  111. }
  112. public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e) 
  113. {
  114. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  115. {
  116. string NotificationID = DataBinder.Eval(e.Item.DataItem, "NotificationID").ToString();
  117. string PersonID = DataBinder.Eval(e.Item.DataItem, "PersonID").ToString();
  118. LinkButton myDeleteButton;
  119. myDeleteButton = (LinkButton) e.Item.Cells[0].Controls[0];
  120. myDeleteButton.Attributes.Add("onclick", @"return confirm('你确认要删除此信息吗?');");
  121. SqlCommand MyCommand0 = new SqlCommand("PersonName",MyConnection);
  122. MyCommand0.CommandType = CommandType.StoredProcedure;
  123. MyCommand0.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
  124. MyCommand0.Parameters["@PersonID"].Value = Int32.Parse(PersonID);
  125. if(MyConnection.State.ToString()=="Closed")
  126. MyConnection.Open(); 
  127. SqlDataReader myReader0 =  MyCommand0.ExecuteReader();
  128. myReader0.Read();
  129.                 e.Item.Cells[3].Text = myReader0["Name"].ToString();
  130. myReader0.Close();
  131. MyConnection.Close();
  132. string Contents = DataBinder.Eval(e.Item.DataItem, "Contents").ToString();
  133. if(Contents.Length>15)
  134. Contents = Contents.Substring(0,15)+"......";
  135. e.Item.Cells[2].Text = "<a href = javascript:window.open('Notification.aspx?NotificationID="+NotificationID+"','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,width=300,top=0,height=300');window.close();>"+Contents+"</a>";
  136. }
  137. }
  138.     public void MyDataGrid_ItemCommand(Object sender, DataGridCommandEventArgs e)
  139. {
  140. switch(((LinkButton)e.CommandSource).CommandName)
  141. {
  142. case "Cancel":
  143. Cancel(e);
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. public void Cancel(DataGridCommandEventArgs e)
  150. {
  151. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  152. int itemindex = e.Item.ItemIndex;
  153. SqlCommand MyCommand0 = new SqlCommand("UpdateNotificationState",MyConnection);
  154. MyCommand0.CommandType = CommandType.StoredProcedure;
  155. MyCommand0.Parameters.Add(new SqlParameter("@NotificationID", SqlDbType.Int, 4));
  156. MyCommand0.Parameters["@NotificationID"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];
  157. if(MyConnection.State.ToString()=="Closed")
  158. MyConnection.Open(); 
  159. MyCommand0.ExecuteNonQuery();
  160. MyConnection.Close();
  161. MyDataGrid.DataSource = CreateDataSource();
  162. MyDataGrid.DataBind();
  163. RegisterStartupScript("alert","<script>alert('取消成功!')</script>");
  164. }
  165. private void ShowStats() 
  166. {
  167. if (totalNotification == 0) 
  168. {
  169. MyDataGrid.Visible = false;
  170. stats.Text = "未找到符合条件的记录!";
  171. }
  172. else 
  173. {
  174. MyDataGrid.Visible = true;
  175. int startOffset = (totalNotification > 0) ? 
  176. (MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize+1) : 0;
  177. int pageEndOffset = (MyDataGrid.CurrentPageIndex+1)*(MyDataGrid.PageSize);
  178. int endOffset = (pageEndOffset > totalNotification) ? totalNotification : pageEndOffset;
  179. stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录",
  180. startOffset,endOffset,totalNotification);
  181. MyDataGrid.PagerStyle.Visible = (totalNotification <= MyDataGrid.PageSize) ? false : true;
  182. }
  183. }
  184. #region Web 窗体设计器生成的代码
  185. override protected void OnInit(EventArgs e)
  186. {
  187. //
  188. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  189. //
  190. InitializeComponent();
  191. base.OnInit(e);
  192. }
  193. /// <summary>
  194. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  195. /// 此方法的内容。
  196. /// </summary>
  197. private void InitializeComponent()
  198. {    
  199. this.AddNotification.ServerClick += new System.EventHandler(this.AddNotification_ServerClick);
  200. this.Load += new System.EventHandler(this.Page_Load);
  201. }
  202. #endregion
  203. }
  204. }