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

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. /// ManageSchedule 的摘要说明。
  16. /// </summary>
  17. public class ManageSchedule : System.Web.UI.Page
  18. {
  19. protected System.Data.SqlClient.SqlConnection MyConnection;
  20. protected System.Web.UI.WebControls.Label stats;
  21. private int totalSchedule;
  22. protected System.Web.UI.WebControls.DataGrid MyDataGrid;
  23. public string PID;
  24. private void Page_Load(object sender, System.EventArgs e)
  25. {
  26. CheckUser CHU = new CheckUser();
  27. CHU.UserName = User.Identity.Name;
  28. PID = CHU.UserID.ToString();
  29. if(CHU.Is_FileManager() == false)
  30. Response.Write("<script>alert('对不起,你没有查看此页面的权限!');history.back(2)</" + "script>");
  31. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  32. if(!IsPostBack)
  33. {
  34. MyDataGrid.DataSource = CreateDataSource();
  35. MyDataGrid.DataBind();
  36. }
  37. // 在此处放置用户代码以初始化页面
  38. }
  39. private ICollection CreateDataSource() 
  40. {
  41. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  42. SqlDataAdapter MyCommand = new SqlDataAdapter("SelectAllSchedule",MyConnection);
  43. MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
  44.             DataSet ds = new DataSet();
  45. MyCommand.Fill(ds,"Schedule");
  46. MyConnection.Close();
  47. DataView dv = ds.Tables[0].DefaultView;
  48. totalSchedule=ds.Tables[0].Rows.Count;
  49. if (Session["tableAllSchedule"] == null)
  50. Session["tableAllSchedule"] = ds.Tables[0];
  51. if (Session["sorterAllSchedule"] == null)
  52. {
  53. Session["sorterAllSchedule"] ="Date";
  54. Session["sortAllSchedule"]=" DESC";
  55. }
  56. dv.Sort = (String) Session["sorterAllSchedule"]+(String) Session["sortAllSchedule"];
  57. ShowStats();
  58. return dv;
  59. }
  60. public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) 
  61. {
  62. DataGrid gridThatCausedEvent = (DataGrid) sender;
  63. gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
  64. gridThatCausedEvent.DataSource = CreateDataSource();
  65. gridThatCausedEvent.DataBind();
  66. }
  67. public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e) 
  68. {
  69. Session["sorterAllSchedule"] = (string)e.SortExpression;
  70. Session["sortAllSchedule"]=(Session["sortAllSchedule"].ToString()==" ASC")?
  71. " DESC":" ASC";
  72. MyDataGrid.DataSource = CreateDataSource();
  73. MyDataGrid.DataBind();
  74. foreach (DataGridItem item in MyDataGrid.Controls[0].Controls)
  75. {
  76. if (item.ItemType == ListItemType.Header)
  77. {
  78. Label lb =(Label)item.FindControl((string)Session["sorterAllSchedule"]);
  79. lb.Text = (Session["sortAllSchedule"].ToString()==" DESC")?
  80. "↓":"↑";
  81. }
  82. }
  83. }
  84. private void ShowStats() 
  85. {
  86. if (totalSchedule == 0) 
  87. {
  88. MyDataGrid.Visible = false;
  89. stats.Text = "未找到符合条件的记录!";
  90. }
  91. else 
  92. {
  93. MyDataGrid.Visible = true;
  94. int startOffset = (totalSchedule > 0) ? 
  95. (MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize+1) : 0;
  96. int pageEndOffset = (MyDataGrid.CurrentPageIndex+1)*(MyDataGrid.PageSize);
  97. int endOffset = (pageEndOffset > totalSchedule) ? totalSchedule : pageEndOffset;
  98. string SortName;
  99. switch(Session["sorterAllSchedule"].ToString())
  100. {
  101. case "Contents":
  102. SortName = "日程计划";
  103. break;
  104. case "Date":
  105. SortName="编制时间";
  106. break;
  107. case "PersonID":
  108. SortName = "制作人";
  109. break;
  110. case "PlanTime":
  111. SortName = "预定时间";
  112. break;
  113. case "Result":
  114. SortName = "计划状态";
  115. break;
  116. default:
  117. SortName="编制时间";
  118. break;
  119. }
  120. stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录  根据</B>{3}<B>排序</B>",
  121. startOffset,endOffset,totalSchedule,SortName);
  122. MyDataGrid.PagerStyle.Visible = (totalSchedule <= MyDataGrid.PageSize) ? false : true;
  123. }
  124. }
  125. public void MyDataGrid_ItemCreated(Object sender, DataGridItemEventArgs e) 
  126. {
  127. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  128. {
  129. }
  130. if((e.Item.ItemType == ListItemType.Header))
  131. {
  132. for(int i = 0; i<5; i++)
  133. {
  134. Label lb = new Label();
  135. if(i==0)
  136. lb.ID = "Contents";
  137. if(i==1)
  138. lb.ID = "PlanTime";
  139. if(i==2)
  140. lb.ID = "Date";
  141. if(i==3)
  142. lb.ID = "PersonID";
  143. if(i==4)
  144. lb.ID = "Result";
  145. lb.Text = "↓";
  146. e.Item.Cells[i].Controls.Add(lb);
  147. }
  148. }         
  149. }
  150. public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e) 
  151. {
  152. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  153. {
  154. SqlCommand MyCommand1 = new SqlCommand("PersonName",MyConnection);
  155. MyCommand1.CommandType = CommandType.StoredProcedure;
  156. MyCommand1.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
  157. MyCommand1.Parameters["@PersonID"].Value = e.Item.Cells[3].Text;
  158. if(MyConnection.State.ToString()=="Closed")
  159. MyConnection.Open(); 
  160. SqlDataReader myReader1 =  MyCommand1.ExecuteReader();
  161. myReader1.Read();
  162. e.Item.Cells[3].Text = myReader1["Name"].ToString();
  163. myReader1.Close();
  164. MyConnection.Close();
  165.  
  166. if(e.Item.Cells[0].Text.Length>15)
  167. e.Item.Cells[0].Text = e.Item.Cells[0].Text.Substring(0,15)+"......";
  168. LinkButton myDeleteButton;
  169. myDeleteButton = (LinkButton) e.Item.Cells[5].Controls[0];
  170. myDeleteButton.Attributes.Add("onclick", @"return confirm('你确认要删除这条记录吗?');");
  171. }
  172. }
  173. public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E) 
  174. {
  175. SqlCommand MyCommand = new SqlCommand("DeleteSchedule",MyConnection);
  176. MyCommand.CommandType = CommandType.StoredProcedure;
  177. MyCommand.Parameters.Add(new SqlParameter("@ScheduleID", SqlDbType.Int, 4));
  178. MyCommand.Parameters["@ScheduleID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
  179. if(MyConnection.State.ToString()=="Closed")
  180. MyConnection.Open(); 
  181. try
  182. {
  183. MyCommand.ExecuteNonQuery();
  184. }
  185. catch (SqlException)
  186. {
  187. RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
  188. }
  189. MyConnection.Close();
  190. if ((MyDataGrid.CurrentPageIndex!=0)&&((int)E.Item.ItemIndex==0))
  191. {
  192. if (MyDataGrid.Items.Count==1)
  193. MyDataGrid.CurrentPageIndex-=1;
  194. }
  195. MyDataGrid.DataSource = CreateDataSource();
  196. MyDataGrid.DataBind();
  197. Response.Write("<script>opener.location.href=opener.location.href;opener=null;window.close();</"+"script>");
  198. }
  199. #region Web 窗体设计器生成的代码
  200. override protected void OnInit(EventArgs e)
  201. {
  202. //
  203. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  204. //
  205. InitializeComponent();
  206. base.OnInit(e);
  207. }
  208. /// <summary>
  209. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  210. /// 此方法的内容。
  211. /// </summary>
  212. private void InitializeComponent()
  213. {    
  214. this.Load += new System.EventHandler(this.Page_Load);
  215. }
  216. #endregion
  217. }
  218. }