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

页面代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterPagingSortingDelete.aspx.cs" Inherits="RepeaterPagingSortingDelete" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>RepeaterPagingSortingDelete</title>
  6.     <script language="javascript" type="text/javascript">
  7.         function chooseAll(sender)
  8.         {
  9.             var inputs = document.all.tags("INPUT");
  10.             // 遍历页面上所有的 input 
  11.             for (var i = 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.         
  23.         function highLightSelected(chkSelect)
  24.         { 
  25.             if (chkSelect.checked) 
  26.                 chkSelect.parentElement.parentElement.style.backgroundColor='#CCCCFF'
  27.             else 
  28.                 chkSelect.parentElement.parentElement.style.backgroundColor='white'
  29.         }
  30.     </script>
  31. </head>
  32. <body>
  33.     <form id="form1" runat="server">
  34.     <div>
  35.         <asp:LinkButton id="lbtnFirst" runat="server" CommandName="First" OnCommand="lbtnPage_Command">|<</asp:LinkButton>  
  36.         <asp:LinkButton id="lbtnPrevious" runat="server" CommandName="Previous" OnCommand="lbtnPage_Command"><<</asp:LinkButton>  
  37.         <asp:Label id="lblMessage" runat="server" />  
  38.         <asp:LinkButton id="lbtnNext" runat="server" CommandName="Next" OnCommand="lbtnPage_Command">>></asp:LinkButton>  
  39.         <asp:LinkButton id="lbtnLast" runat="server" CommandName="Last" OnCommand="lbtnPage_Command">>|</asp:LinkButton>  
  40.         转到第<asp:DropDownList ID="dropPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dropPage_SelectedIndexChanged"></asp:DropDownList>页        
  41.         <asp:Repeater ID="rptProducts" runat="server" 
  42.             onitemcreated="rptProducts_ItemCreated" 
  43.             onitemdatabound="rptProducts_ItemDataBound">
  44.             <HeaderTemplate>
  45.                 <table border="1">
  46.                 <tr>
  47.                     <td id="tdAll" runat="server"><asp:CheckBox ID="chkAll" runat="server" /></td>
  48.                     <td id="tdProductID" runat="server"><asp:LinkButton ID="lbtnProductID" runat="server" Text="ProductID" CommandName="ProductID" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblHeadProductID" runat="server" Visible="false"></asp:Label></td>
  49.                     <td id="tdProductName" runat="server"><asp:LinkButton ID="lbtnProductName" runat="server" Text="ProductName" CommandName="ProductName" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblProductName" runat="server" Visible="false"></asp:Label></td>
  50.                     <td id="tdSupplierID" runat="server"><asp:LinkButton ID="lbtnSupplierID" runat="server" Text="SupplierID" CommandName="SupplierID" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblSupplierID" runat="server" Visible="false"></asp:Label></td>
  51.                     <td id="tdCategoryID" runat="server"><asp:LinkButton ID="lbtnCategoryID" runat="server" Text="CategoryID" CommandName="CategoryID" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblCategoryID" runat="server" Visible="false"></asp:Label></td>
  52.                     <td id="tdQuantityPerUnit" runat="server"><asp:LinkButton ID="lbtnQuantityPerUnit" runat="server" Text="QuantityPerUnit" CommandName="QuantityPerUnit" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblQuantityPerUnit" runat="server" Visible="false"></asp:Label></td>
  53.                     <td id="tdUnitPrice" runat="server"><asp:LinkButton ID="lbtnUnitPrice" runat="server" Text="UnitPrice" CommandName="UnitPrice" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblUnitPrice" runat="server" Visible="false"></asp:Label></td>
  54.                     <td id="tdUnitsInStock" runat="server"><asp:LinkButton ID="lbtnUnitsInStock" runat="server" Text="UnitsInStock" CommandName="UnitsInStock" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblUnitsInStock" runat="server" Visible="false"></asp:Label></td>
  55.                     <td id="tdUnitsOnOrder" runat="server"><asp:LinkButton ID="lbtnUnitsOnOrder" runat="server" Text="UnitsOnOrder" CommandName="UnitsOnOrder" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblUnitsOnOrder" runat="server" Visible="false"></asp:Label></td>
  56.                     <td id="tdReorderLevel" runat="server"><asp:LinkButton ID="lbtnReorderLevel" runat="server" Text="ReorderLevel" CommandName="ReorderLevel" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblReorderLevel" runat="server" Visible="false"></asp:Label></td>
  57.                     <td id="tdDiscontinued" runat="server"><asp:LinkButton ID="lbtnDiscontinued" runat="server" Text="Discontinued" CommandName="Discontinued" OnCommand="lbtnSort_Command"></asp:LinkButton><asp:Label ID="lblDiscontinued" runat="server" Visible="false"></asp:Label></td>
  58.                 </tr>
  59.             </HeaderTemplate>
  60.             <ItemTemplate>
  61.                 <tr>
  62.                     <td id="tdSelect" runat="server"><asp:CheckBox ID="chkSelect" runat="server" /></td>
  63.                     <td><asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label></td>
  64.                     <td><asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label></td>
  65.                     <td><asp:Label ID="lblSupplierID" runat="server" Text='<%# Eval("SupplierID") %>'></asp:Label></td>
  66.                     <td><asp:Label ID="lblCategoryID" runat="server" Text='<%# Eval("CategoryID") %>'></asp:Label></td>
  67.                     <td><asp:Label ID="lblQuantityPerUnit" runat="server" Text='<%# Eval("QuantityPerUnit") %>'></asp:Label></td>
  68.                     <td><asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label></td>
  69.                     <td><asp:Label ID="lblUnitsInStock" runat="server" Text='<%# Eval("UnitsInStock") %>'></asp:Label></td>
  70.                     <td><asp:Label ID="lblUnitsOnOrder" runat="server" Text='<%# Eval("UnitsOnOrder") %>'></asp:Label></td>
  71.                     <td><asp:Label ID="lblReorderLevel" runat="server" Text='<%# Eval("ReorderLevel") %>'></asp:Label></td>
  72.                     <td><asp:CheckBox ID="chkDiscontinued" runat="server" Checked='<%# Eval("Discontinued") %>' Enabled="false" /></td>
  73.                 </tr>
  74.             </ItemTemplate>
  75.             <FooterTemplate>
  76.                 </table>
  77.             </FooterTemplate>
  78.         </asp:Repeater>
  79.         <asp:Button ID="btnDelete" runat="server" Text="删除" onclick="btnDelete_Click" />
  80.     </div>
  81.     </form>
  82. </body>
  83. </html>

后台代码:

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.SqlClient;
  14. public partial class RepeaterPagingSortingDelete : System.Web.UI.Page
  15. {
  16.     //每页显示的最多记录的条数
  17.     private int pageSize = 10;
  18.     //当前页号
  19.     private int currentPageNumber;
  20.     //排序表达式
  21.     private string sortExpression = string.Empty;
  22.     //排序方向
  23.     private string sortDirection = string.Empty;
  24.     //显示数据的总条数
  25.     private static int rowCount;
  26.     //总页数
  27.     private static int pageCount;
  28.     protected void Page_Load(object sender, EventArgs e)
  29.     {
  30.         if (!IsPostBack)
  31.         {
  32.             SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
  33.             SqlCommand cmd = new SqlCommand("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 (int i = 1; i <= pageCount; i++)
  41.             {
  42.                 dropPage.Items.Add(new ListItem(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.     private void Query()
  52.     {
  53.         SetButton(currentPageNumber);
  54.         SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
  55.         SqlCommand cmd = new SqlCommand("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.     protected void lbtnPage_Command(object sender, CommandEventArgs e)
  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.     protected void lbtnSort_Command(object sender, CommandEventArgs e)
  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.             else if (sortDirection == "DESC" || sortDirection == string.Empty)
  100.             {
  101.                 sortDirection = "ASC";
  102.             }
  103.         }
  104.         sortExpression = e.CommandName;
  105.         Query();
  106.     }
  107.     protected void dropPage_SelectedIndexChanged(object sender, EventArgs e)
  108.     {
  109.         currentPageNumber = int.Parse(dropPage.SelectedValue);
  110.         Query();
  111.     }
  112.     private void SetButton(int currentPageNumber)
  113.     {
  114.         lbtnFirst.Enabled = currentPageNumber != 1;
  115.         lbtnPrevious.Enabled = currentPageNumber != 1;
  116.         lbtnNext.Enabled = currentPageNumber != pageCount;
  117.         lbtnLast.Enabled = currentPageNumber != pageCount;
  118.     }
  119.     private void Save()
  120.     {
  121.         ViewState["currentPageNumber"] = currentPageNumber;
  122.         ViewState["sortExpression"] = sortExpression;
  123.         ViewState["sortDirection"] = sortDirection;
  124.     }
  125.     protected void rptProducts_ItemCreated(object sender, RepeaterItemEventArgs e)
  126.     {
  127.         if (e.Item.ItemType == ListItemType.Header)
  128.         {
  129.             if (!string.IsNullOrEmpty(sortDirection))
  130.             {
  131.                 Label lblSort = new Label();
  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) as HtmlTableCell).Controls.Add(lblSort);
  137.             }
  138.         }
  139.     }
  140.     protected void btnDelete_Click(object sender, EventArgs e)
  141.     {
  142.         CheckBox chkSelect;
  143.         Label lblProductID;
  144.         foreach (RepeaterItem item in rptProducts.Items)
  145.         {
  146.             chkSelect = item.FindControl("chkSelect"as CheckBox;
  147.             if (chkSelect != null && chkSelect.Checked)
  148.             {
  149.                 lblProductID = item.FindControl("lblProductID"as Label;
  150.                 //Response.Write(lblProductID.Text);
  151.                 //可以在这里完成删除逻辑
  152.             }
  153.         }
  154.     }
  155.     protected void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
  156.     {
  157.         if (e.Item.ItemType == ListItemType.Header)
  158.         { 
  159.             CheckBox chkAll = e.Item.FindControl("chkAll"as CheckBox;
  160.             chkAll.Attributes.Add("onclick""chooseAll('" + chkAll.ClientID + "')");        
  161.         }
  162.         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  163.         {
  164.             CheckBox chkSelect = e.Item.FindControl("chkSelect"as CheckBox;
  165.             chkSelect.Attributes.Add("onclick""highLightSelected(this);");
  166.         }
  167.     }
  168. }

转载于:https://www.cnblogs.com/zgqys1980/archive/2009/04/21/1440577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值