Repeater自定义分页 + 排序 + 全选删除(3)

页面代码:

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="RepeaterPagingSortingDelete.aspx.cs"Inherits="RepeaterPagingSortingDelete"%>

  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>RepeaterPagingSortingDelete</title>
  6. <scriptlanguage="javascript"type="text/javascript">
  7. functionchooseAll(sender)
  8. {
  9. varinputs=document.all.tags("INPUT");
  10. //遍历页面上所有的input
  11. for(vari=0;i<inputs.length;i++)
  12. {
  13. //如果此input元素的类型为checkbox,并且其id中包含chkSelect
  14. if(inputs[i].type=="checkbox"&&inputs[i].id.indexOf("chkSelect")>=0)
  15. {
  16. //设置此复选框的checked与全选复选框相同
  17. inputs[i].checked=document.getElementById(sender).checked;
  18. inputs[i].onclick();
  19. }
  20. }
  21. }
  22. functionhighLightSelected(chkSelect)
  23. {
  24. if(chkSelect.checked)
  25. chkSelect.parentElement.parentElement.style.backgroundColor='#CCCCFF';
  26. else
  27. chkSelect.parentElement.parentElement.style.backgroundColor='white';
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <formid="form1"runat="server">
  33. <div>
  34. <asp:LinkButtonid="lbtnFirst"runat="server"CommandName="First"OnCommand="lbtnPage_Command">|<</asp:LinkButton>
  35. <asp:LinkButtonid="lbtnPrevious"runat="server"CommandName="Previous"OnCommand="lbtnPage_Command"><<</asp:LinkButton>
  36. <asp:Labelid="lblMessage"runat="server"/>
  37. <asp:LinkButtonid="lbtnNext"runat="server"CommandName="Next"OnCommand="lbtnPage_Command">>></asp:LinkButton>
  38. <asp:LinkButtonid="lbtnLast"runat="server"CommandName="Last"OnCommand="lbtnPage_Command">>|</asp:LinkButton>
  39. 转到第<asp:DropDownListID="dropPage"runat="server"AutoPostBack="True"OnSelectedIndexChanged="dropPage_SelectedIndexChanged"></asp:DropDownList>
  40. <asp:RepeaterID="rptProducts"runat="server"
  41. onitemcreated="rptProducts_ItemCreated"
  42. onitemdatabound="rptProducts_ItemDataBound">
  43. <HeaderTemplate>
  44. <tableborder="1">
  45. <tr>
  46. <tdid="tdAll"runat="server"><asp:CheckBoxID="chkAll"runat="server"/></td>
  47. <tdid="tdProductID"runat="server"><asp:LinkButtonID="lbtnProductID"runat="server"Text="ProductID"CommandName="ProductID"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblHeadProductID"runat="server"Visible="false"></asp:Label></td>
  48. <tdid="tdProductName"runat="server"><asp:LinkButtonID="lbtnProductName"runat="server"Text="ProductName"CommandName="ProductName"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblProductName"runat="server"Visible="false"></asp:Label></td>
  49. <tdid="tdSupplierID"runat="server"><asp:LinkButtonID="lbtnSupplierID"runat="server"Text="SupplierID"CommandName="SupplierID"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblSupplierID"runat="server"Visible="false"></asp:Label></td>
  50. <tdid="tdCategoryID"runat="server"><asp:LinkButtonID="lbtnCategoryID"runat="server"Text="CategoryID"CommandName="CategoryID"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblCategoryID"runat="server"Visible="false"></asp:Label></td>
  51. <tdid="tdQuantityPerUnit"runat="server"><asp:LinkButtonID="lbtnQuantityPerUnit"runat="server"Text="QuantityPerUnit"CommandName="QuantityPerUnit"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblQuantityPerUnit"runat="server"Visible="false"></asp:Label></td>
  52. <tdid="tdUnitPrice"runat="server"><asp:LinkButtonID="lbtnUnitPrice"runat="server"Text="UnitPrice"CommandName="UnitPrice"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblUnitPrice"runat="server"Visible="false"></asp:Label></td>
  53. <tdid="tdUnitsInStock"runat="server"><asp:LinkButtonID="lbtnUnitsInStock"runat="server"Text="UnitsInStock"CommandName="UnitsInStock"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblUnitsInStock"runat="server"Visible="false"></asp:Label></td>
  54. <tdid="tdUnitsOnOrder"runat="server"><asp:LinkButtonID="lbtnUnitsOnOrder"runat="server"Text="UnitsOnOrder"CommandName="UnitsOnOrder"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblUnitsOnOrder"runat="server"Visible="false"></asp:Label></td>
  55. <tdid="tdReorderLevel"runat="server"><asp:LinkButtonID="lbtnReorderLevel"runat="server"Text="ReorderLevel"CommandName="ReorderLevel"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblReorderLevel"runat="server"Visible="false"></asp:Label></td>
  56. <tdid="tdDiscontinued"runat="server"><asp:LinkButtonID="lbtnDiscontinued"runat="server"Text="Discontinued"CommandName="Discontinued"OnCommand="lbtnSort_Command"></asp:LinkButton><asp:LabelID="lblDiscontinued"runat="server"Visible="false"></asp:Label></td>
  57. </tr>
  58. </HeaderTemplate>
  59. <ItemTemplate>
  60. <tr>
  61. <tdid="tdSelect"runat="server"><asp:CheckBoxID="chkSelect"runat="server"/></td>
  62. <td><asp:LabelID="lblProductID"runat="server"Text='<%#Eval("ProductID")%>'></asp:Label></td>
  63. <td><asp:LabelID="lblProductName"runat="server"Text='<%#Eval("ProductName")%>'></asp:Label></td>
  64. <td><asp:LabelID="lblSupplierID"runat="server"Text='<%#Eval("SupplierID")%>'></asp:Label></td>
  65. <td><asp:LabelID="lblCategoryID"runat="server"Text='<%#Eval("CategoryID")%>'></asp:Label></td>
  66. <td><asp:LabelID="lblQuantityPerUnit"runat="server"Text='<%#Eval("QuantityPerUnit")%>'></asp:Label></td>
  67. <td><asp:LabelID="lblUnitPrice"runat="server"Text='<%#Eval("UnitPrice")%>'></asp:Label></td>
  68. <td><asp:LabelID="lblUnitsInStock"runat="server"Text='<%#Eval("UnitsInStock")%>'></asp:Label></td>
  69. <td><asp:LabelID="lblUnitsOnOrder"runat="server"Text='<%#Eval("UnitsOnOrder")%>'></asp:Label></td>
  70. <td><asp:LabelID="lblReorderLevel"runat="server"Text='<%#Eval("ReorderLevel")%>'></asp:Label></td>
  71. <td><asp:CheckBoxID="chkDiscontinued"runat="server"Checked='<%#Eval("Discontinued")%>'Enabled="false"/></td>
  72. </tr>
  73. </ItemTemplate>
  74. <FooterTemplate>
  75. </table>
  76. </FooterTemplate>
  77. </asp:Repeater>
  78. <asp:ButtonID="btnDelete"runat="server"Text="删除"onclick="btnDelete_Click"/>
  79. </div>
  80. </form>
  81. </body>
  82. </html>

后台代码:

  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.Configuration;
  4. usingSystem.Data;
  5. usingSystem.Linq;
  6. usingSystem.Web;
  7. usingSystem.Web.Security;
  8. usingSystem.Web.UI;
  9. usingSystem.Web.UI.HtmlControls;
  10. usingSystem.Web.UI.WebControls;
  11. usingSystem.Web.UI.WebControls.WebParts;
  12. usingSystem.Xml.Linq;
  13. usingSystem.Data.SqlClient;

  14. publicpartialclassRepeaterPagingSortingDelete:System.Web.UI.Page
  15. {
  16. //每页显示的最多记录的条数
  17. privateintpageSize=10;
  18. //当前页号
  19. privateintcurrentPageNumber;
  20. //排序表达式
  21. privatestringsortExpression=string.Empty;
  22. //排序方向
  23. privatestringsortDirection=string.Empty;
  24. //显示数据的总条数
  25. privatestaticintrowCount;
  26. //总页数
  27. privatestaticintpageCount;

  28. protectedvoidPage_Load(objectsender,EventArgse)
  29. {
  30. if(!IsPostBack)
  31. {
  32. SqlConnectioncn=newSqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
  33. SqlCommandcmd=newSqlCommand("GetProductsCount",cn);
  34. cmd.CommandType=CommandType.StoredProcedure;
  35. cn.Open();
  36. rowCount=(int)cmd.ExecuteScalar();
  37. cn.Close();
  38. pageCount=(rowCount-1)/pageSize+1;
  39. currentPageNumber=1;
  40. for(inti=1;i<=pageCount;i++)
  41. {
  42. dropPage.Items.Add(newListItem(i.ToString(),i.ToString()));
  43. }
  44. dropPage.SelectedValue=dropPage.Items.FindByValue(currentPageNumber.ToString()).Value;
  45. Query();
  46. }
  47. sortExpression=ViewState["sortExpression"].ToString();
  48. sortDirection=ViewState["sortDirection"].ToString();
  49. currentPageNumber=Convert.ToInt32(ViewState["currentPageNumber"]);
  50. }

  51. privatevoidQuery()
  52. {
  53. SetButton(currentPageNumber);
  54. SqlConnectioncn=newSqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
  55. SqlCommandcmd=newSqlCommand("GetProductsByPage",cn);
  56. cmd.CommandType=CommandType.StoredProcedure;
  57. cmd.Parameters.AddWithValue("@sortExpression",sortExpression+""+sortDirection);
  58. cmd.Parameters.AddWithValue("@pageNumber",currentPageNumber);
  59. cmd.Parameters.AddWithValue("@pageSize",pageSize);
  60. cn.Open();
  61. rptProducts.DataSource=cmd.ExecuteReader();
  62. rptProducts.DataBind();
  63. cn.Close();
  64. lblMessage.Text="共找到"+rowCount+"条记录,当前第"+currentPageNumber+"/"+pageCount+"页";
  65. Save();
  66. }

  67. protectedvoidlbtnPage_Command(objectsender,CommandEventArgse)
  68. {
  69. switch(e.CommandName)
  70. {
  71. case"First":
  72. currentPageNumber=1;
  73. break;
  74. case"Previous":
  75. currentPageNumber=(int)ViewState["currentPageNumber"]-1>1?(int)ViewState["currentPageNumber"]-1:1;
  76. break;
  77. case"Next":
  78. currentPageNumber=(int)ViewState["currentPageNumber"]+1<pageCount?(int)ViewState["currentPageNumber"]+1:pageCount;
  79. break;
  80. case"Last":
  81. currentPageNumber=pageCount;
  82. break;
  83. }
  84. dropPage.SelectedValue=dropPage.Items.FindByValue(currentPageNumber.ToString()).Value;
  85. Query();
  86. }

  87. protectedvoidlbtnSort_Command(objectsender,CommandEventArgse)
  88. {
  89. if(e.CommandName!=ViewState["sortExpression"].ToString())
  90. {
  91. sortDirection="ASC";
  92. }
  93. else
  94. {
  95. if(sortDirection=="ASC")
  96. {
  97. sortDirection="DESC";
  98. }
  99. elseif(sortDirection=="DESC"||sortDirection==string.Empty)
  100. {
  101. sortDirection="ASC";
  102. }
  103. }
  104. sortExpression=e.CommandName;
  105. Query();
  106. }

  107. protectedvoiddropPage_SelectedIndexChanged(objectsender,EventArgse)
  108. {
  109. currentPageNumber=int.Parse(dropPage.SelectedValue);
  110. Query();
  111. }

  112. privatevoidSetButton(intcurrentPageNumber)
  113. {
  114. lbtnFirst.Enabled=currentPageNumber!=1;
  115. lbtnPrevious.Enabled=currentPageNumber!=1;
  116. lbtnNext.Enabled=currentPageNumber!=pageCount;
  117. lbtnLast.Enabled=currentPageNumber!=pageCount;
  118. }

  119. privatevoidSave()
  120. {
  121. ViewState["currentPageNumber"]=currentPageNumber;
  122. ViewState["sortExpression"]=sortExpression;
  123. ViewState["sortDirection"]=sortDirection;
  124. }

  125. protectedvoidrptProducts_ItemCreated(objectsender,RepeaterItemEventArgse)
  126. {
  127. if(e.Item.ItemType==ListItemType.Header)
  128. {
  129. if(!string.IsNullOrEmpty(sortDirection))
  130. {
  131. LabellblSort=newLabel();
  132. lblSort.EnableTheming=false;
  133. lblSort.Font.Name="webdings";
  134. lblSort.Font.Size=FontUnit.Small;
  135. lblSort.Text=sortDirection=="ASC"?"5":"6";
  136. (e.Item.FindControl("td"+sortExpression)asHtmlTableCell).Controls.Add(lblSort);
  137. }
  138. }
  139. }
  140. protectedvoidbtnDelete_Click(objectsender,EventArgse)
  141. {
  142. CheckBoxchkSelect;
  143. LabellblProductID;
  144. foreach(RepeaterItemiteminrptProducts.Items)
  145. {
  146. chkSelect=item.FindControl("chkSelect")asCheckBox;
  147. if(chkSelect!=null&&chkSelect.Checked)
  148. {
  149. lblProductID=item.FindControl("lblProductID")asLabel;
  150. //Response.Write(lblProductID.Text);
  151. //可以在这里完成删除逻辑
  152. }
  153. }
  154. }

  155. protectedvoidrptProducts_ItemDataBound(objectsender,RepeaterItemEventArgse)
  156. {
  157. if(e.Item.ItemType==ListItemType.Header)
  158. {
  159. CheckBoxchkAll=e.Item.FindControl("chkAll")asCheckBox;
  160. chkAll.Attributes.Add("onclick","chooseAll('"+chkAll.ClientID+"')");
  161. }

  162. if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
  163. {
  164. CheckBoxchkSelect=e.Item.FindControl("chkSelect")asCheckBox;
  165. chkSelect.Attributes.Add("onclick","highLightSelected(this);");
  166. }
  167. }
  168. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值