ManageInfomation.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. /// ManageInfomation 的摘要说明。
  16. /// </summary>
  17. public class ManageInfomation : System.Web.UI.Page
  18. {
  19. protected System.Data.SqlClient.SqlConnection MyConnection;
  20. private int totalInfomation;
  21. protected System.Web.UI.WebControls.Label stats;
  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. private ICollection CreateDataSource() 
  39. {
  40. MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  41. SqlDataAdapter MyCommand = new SqlDataAdapter("SelectALLInfomation",MyConnection);
  42. MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
  43. DataSet ds = new DataSet();
  44. MyCommand.Fill(ds,"Infomation");
  45. MyConnection.Close();
  46. DataView dv = ds.Tables[0].DefaultView;
  47. totalInfomation=ds.Tables[0].Rows.Count;
  48. if (Session["tableALLInfomation"] == null)
  49. Session["tableALLInfomation"] = ds.Tables[0];
  50. if (Session["sorterALLInfomation"] == null)
  51. {
  52. Session["sorterALLInfomation"] ="Date";
  53. Session["sortALLInfomation"]=" DESC";
  54. }
  55. dv.Sort = (String) Session["sorterALLInfomation"]+(String) Session["sortALLInfomation"];
  56. ShowStats();
  57. return dv;
  58. }
  59. public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) 
  60. {
  61. DataGrid gridThatCausedEvent = (DataGrid) sender;
  62. gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
  63. gridThatCausedEvent.DataSource = CreateDataSource();
  64. gridThatCausedEvent.DataBind();
  65. }
  66. public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e) 
  67. {
  68. Session["sorterALLInfomation"] = (string)e.SortExpression;
  69. Session["sortALLInfomation"]=(Session["sortALLInfomation"].ToString()==" ASC")?
  70. " DESC":" ASC";
  71. MyDataGrid.DataSource = CreateDataSource();
  72. MyDataGrid.DataBind();
  73. foreach (DataGridItem item in MyDataGrid.Controls[0].Controls)
  74. {
  75. if (item.ItemType == ListItemType.Header)
  76. {
  77. Label lb =(Label)item.FindControl((string)Session["sorterALLInfomation"]);
  78. lb.Text = (Session["sortALLInfomation"].ToString()==" DESC")?
  79. "↓":"↑";
  80. }
  81. }
  82. }
  83. private void ShowStats() 
  84. {
  85. if (totalInfomation == 0) 
  86. {
  87. MyDataGrid.Visible = false;
  88. stats.Text = "未找到符合条件的记录!";
  89. }
  90. else 
  91. {
  92. MyDataGrid.Visible = true;
  93. int startOffset = (totalInfomation > 0) ? 
  94. (MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize+1) : 0;
  95. int pageEndOffset = (MyDataGrid.CurrentPageIndex+1)*(MyDataGrid.PageSize);
  96. int endOffset = (pageEndOffset > totalInfomation) ? totalInfomation : pageEndOffset;
  97. string SortName;
  98. switch(Session["sorterALLInfomation"].ToString())
  99. {
  100. case "Title":
  101. SortName = "标题";
  102. break;
  103. case "Date":
  104. SortName="录入时间";
  105. break;
  106. case "Name":
  107. SortName = "录入";
  108. break;
  109. case "Writer":
  110. SortName = "作者";
  111. break;
  112. case "InfoCategoryName":
  113. SortName = "类别";
  114. break;
  115. case "State":
  116. SortName = "浏览次数";
  117. break;
  118. default:
  119. SortName="录入时间";
  120. break;
  121. }
  122. stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录  根据</B>{3}<B>排序</B>",
  123. startOffset,endOffset,totalInfomation,SortName);
  124. MyDataGrid.PagerStyle.Visible = (totalInfomation <= MyDataGrid.PageSize) ? false : true;
  125. }
  126. }
  127. public void MyDataGrid_ItemCreated(Object sender, DataGridItemEventArgs e) 
  128. {
  129. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  130. {
  131. }
  132. if((e.Item.ItemType == ListItemType.Header))
  133. {
  134. for(int i = 0; i<6; i++)
  135. {
  136. Label lb = new Label();
  137. if(i==0)
  138. lb.ID = "Title";
  139. if(i==1)
  140. lb.ID = "Date";
  141. if(i==2)
  142. lb.ID = "Writer";
  143. if(i==3)
  144. lb.ID = "Name";
  145. if(i==4)
  146. lb.ID = "InfoCategoryName";
  147. if(i==5)
  148. lb.ID = "State";
  149. lb.Text = "↓";
  150. e.Item.Cells[i].Controls.Add(lb);
  151. }
  152. }         
  153. }
  154. public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e) 
  155. {
  156. if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
  157. {
  158. string InfoID = DataBinder.Eval(e.Item.DataItem, "InfoID").ToString();
  159. if(e.Item.Cells[0].Text.Length>15)
  160. e.Item.Cells[0].Text = e.Item.Cells[0].Text.Substring(0,15)+"......";
  161. LinkButton myDeleteButton1;
  162. LinkButton myDeleteButton2;
  163. myDeleteButton1 = (LinkButton) e.Item.Cells[6].Controls[0];
  164. myDeleteButton2 = (LinkButton) e.Item.Cells[7].Controls[0];
  165. myDeleteButton1.Attributes.Add("onclick", @"if(confirm('你确认要修改这条记录吗?')) window.open('EditInfo.aspx?InfoID="+InfoID+"','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=600,height=500');return false;");
  166. myDeleteButton2.Attributes.Add("onclick", @"return confirm('你确认要删除这条记录吗?');");
  167. }
  168. }
  169. public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E) 
  170. {
  171. SqlCommand MyCommand = new SqlCommand("DeleteInfomation",MyConnection);
  172. MyCommand.CommandType = CommandType.StoredProcedure;
  173. MyCommand.Parameters.Add(new SqlParameter("@InfoID", SqlDbType.Int, 4));
  174. MyCommand.Parameters["@InfoID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
  175. if(MyConnection.State.ToString()=="Closed")
  176. MyConnection.Open(); 
  177. try
  178. {
  179. MyCommand.ExecuteNonQuery();
  180. }
  181. catch (SqlException)
  182. {
  183. RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
  184. }
  185. MyConnection.Close();
  186. if ((MyDataGrid.CurrentPageIndex!=0)&&((int)E.Item.ItemIndex==0))
  187. {
  188. if (MyDataGrid.Items.Count==1)
  189. MyDataGrid.CurrentPageIndex-=1;
  190. }
  191. MyDataGrid.DataSource = CreateDataSource();
  192. MyDataGrid.DataBind();
  193. }
  194. #region Web 窗体设计器生成的代码
  195. override protected void OnInit(EventArgs e)
  196. {
  197. //
  198. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  199. //
  200. InitializeComponent();
  201. base.OnInit(e);
  202. }
  203. /// <summary>
  204. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  205. /// 此方法的内容。
  206. /// </summary>
  207. private void InitializeComponent()
  208. {    
  209. this.Load += new System.EventHandler(this.Page_Load);
  210. }
  211. #endregion
  212. }
  213. }