代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.NET
】
epeater自定义分页 + 排序 + 全选删除
作者:
睿杰
/ 发布于
2016/1/25
/
830
页面代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterPagingSortingDelete.aspx.cs" Inherits="RepeaterPagingSortingDelete" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>RepeaterPagingSortingDelete</title> <script language="javascript" type="text/javascript"> function chooseAll(sender) { var inputs = document.all.tags("INPUT"); // 遍历页面上所有的 input for (var i = 0; i < inputs.length; i++) { //如果此input元素的类型为checkbox,并且其id中包含chkSelect if (inputs[i].type == "checkbox" && inputs[i].id.indexOf("chkSelect") >= 0) { //设置此复选框的checked与全选复选框相同 inputs[i].checked = document.getElementById(sender).checked; inputs[i].onclick(); } } } function highLightSelected(chkSelect) { if (chkSelect.checked) chkSelect.parentElement.parentElement.style.backgroundColor='#CCCCFF'; else chkSelect.parentElement.parentElement.style.backgroundColor='white'; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:LinkButton id="lbtnFirst" runat="server" CommandName="First" OnCommand="lbtnPage_Command">|<</asp:LinkButton> <asp:LinkButton id="lbtnPrevious" runat="server" CommandName="Previous" OnCommand="lbtnPage_Command"><<</asp:LinkButton> <asp:Label id="lblMessage" runat="server" /> <asp:LinkButton id="lbtnNext" runat="server" CommandName="Next" OnCommand="lbtnPage_Command">>></asp:LinkButton> <asp:LinkButton id="lbtnLast" runat="server" CommandName="Last" OnCommand="lbtnPage_Command">>|</asp:LinkButton> 转到第<asp:DropDownList ID="dropPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dropPage_SelectedIndexChanged"></asp:DropDownList>页 <asp:Repeater ID="rptProducts" runat="server" onitemcreated="rptProducts_ItemCreated" onitemdatabound="rptProducts_ItemDataBound"> <HeaderTemplate> <table border="1"> <tr> <td id="tdAll" runat="server"><asp:CheckBox ID="chkAll" runat="server" /></td> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td id="tdSelect" runat="server"><asp:CheckBox ID="chkSelect" runat="server" /></td> <td><asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label></td> <td><asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label></td> <td><asp:Label ID="lblSupplierID" runat="server" Text='<%# Eval("SupplierID") %>'></asp:Label></td> <td><asp:Label ID="lblCategoryID" runat="server" Text='<%# Eval("CategoryID") %>'></asp:Label></td> <td><asp:Label ID="lblQuantityPerUnit" runat="server" Text='<%# Eval("QuantityPerUnit") %>'></asp:Label></td> <td><asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label></td> <td><asp:Label ID="lblUnitsInStock" runat="server" Text='<%# Eval("UnitsInStock") %>'></asp:Label></td> <td><asp:Label ID="lblUnitsOnOrder" runat="server" Text='<%# Eval("UnitsOnOrder") %>'></asp:Label></td> <td><asp:Label ID="lblReorderLevel" runat="server" Text='<%# Eval("ReorderLevel") %>'></asp:Label></td> <td><asp:CheckBox ID="chkDiscontinued" runat="server" Checked='<%# Eval("Discontinued") %>' Enabled="false" /></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:Button ID="btnDelete" runat="server" Text="删除" onclick="btnDelete_Click" /> </div> </form> </body> </html> 后台代码: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class RepeaterPagingSortingDelete : System.Web.UI.Page { //每页显示的最多记录的条数 private int pageSize = 10; //当前页号 private int currentPageNumber; //排序表达式 private string sortExpression = string.Empty; //排序方向 private string sortDirection = string.Empty; //显示数据的总条数 private static int rowCount; //总页数 private static int pageCount; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("GetProductsCount", cn); cmd.CommandType = CommandType.StoredProcedure; cn.Open(); rowCount = (int)cmd.ExecuteScalar(); cn.Close(); pageCount = (rowCount - 1) / pageSize + 1; currentPageNumber = 1; for (int i = 1; i <= pageCount; i++) { dropPage.Items.Add(new ListItem(i.ToString(), i.ToString())); } dropPage.SelectedValue = dropPage.Items.FindByValue(currentPageNumber.ToString()).Value; Query(); } sortExpression = ViewState["sortExpression"].ToString(); sortDirection = ViewState["sortDirection"].ToString(); currentPageNumber = Convert.ToInt32(ViewState["currentPageNumber"]); } private void Query() { SetButton(currentPageNumber); SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("GetProductsByPage", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@sortExpression", sortExpression + " " + sortDirection); cmd.Parameters.AddWithValue("@pageNumber", currentPageNumber); cmd.Parameters.AddWithValue("@pageSize", pageSize); cn.Open(); rptProducts.DataSource = cmd.ExecuteReader(); rptProducts.DataBind(); cn.Close(); lblMessage.Text = "共找到" + rowCount + "条记录, 当前第" + currentPageNumber + "/" + pageCount + "页"; Save(); } protected void lbtnPage_Command(object sender, CommandEventArgs e) { switch (e.CommandName) { case "First": currentPageNumber = 1; break; case "Previous": currentPageNumber = (int)ViewState["currentPageNumber"] - 1 > 1 ? (int)ViewState["currentPageNumber"] - 1 : 1; break; case "Next": currentPageNumber = (int)ViewState["currentPageNumber"] + 1 < pageCount ? (int)ViewState["currentPageNumber"] + 1 : pageCount; break; case "Last": currentPageNumber = pageCount; break; } dropPage.SelectedValue = dropPage.Items.FindByValue(currentPageNumber.ToString()).Value; Query(); } protected void lbtnSort_Command(object sender, CommandEventArgs e) { if (e.CommandName != ViewState["sortExpression"].ToString()) { sortDirection = "ASC"; } else { if (sortDirection == "ASC") { sortDirection = "DESC"; } else if (sortDirection == "DESC" || sortDirection == string.Empty) { sortDirection = "ASC"; } } sortExpression = e.CommandName; Query(); } protected void dropPage_SelectedIndexChanged(object sender, EventArgs e) { currentPageNumber = int.Parse(dropPage.SelectedValue); Query(); } private void SetButton(int currentPageNumber) { lbtnFirst.Enabled = currentPageNumber != 1; lbtnPrevious.Enabled = currentPageNumber != 1; lbtnNext.Enabled = currentPageNumber != pageCount; lbtnLast.Enabled = currentPageNumber != pageCount; } private void Save() { ViewState["currentPageNumber"] = currentPageNumber; ViewState["sortExpression"] = sortExpression; ViewState["sortDirection"] = sortDirection; } protected void rptProducts_ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { if (!string.IsNullOrEmpty(sortDirection)) { Label lblSort = new Label(); lblSort.EnableTheming = false; lblSort.Font.Name = "webdings"; lblSort.Font.Size = FontUnit.Small; lblSort.Text = sortDirection == "ASC" ? "5" : "6"; (e.Item.FindControl("td" + sortExpression) as HtmlTableCell).Controls.Add(lblSort); } } } protected void btnDelete_Click(object sender, EventArgs e) { CheckBox chkSelect; Label lblProductID; foreach (RepeaterItem item in rptProducts.Items) { chkSelect = item.FindControl("chkSelect") as CheckBox; if (chkSelect != null && chkSelect.Checked) { lblProductID = item.FindControl("lblProductID") as Label; //Response.Write(lblProductID.Text); //可以在这里完成删除逻辑 } } } protected void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { CheckBox chkAll = e.Item.FindControl("chkAll") as CheckBox; chkAll.Attributes.Add("onclick", "chooseAll('" + chkAll.ClientID + "')"); } if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { CheckBox chkSelect = e.Item.FindControl("chkSelect") as CheckBox; chkSelect.Attributes.Add("onclick", "highLightSelected(this);"); } } }
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
睿杰
贡献的其它代码
(
15
)
.
Python数据操作方法封装类
.
SQL server 数据库危险存储过程删除与恢复
.
按文章词频提取关键字
.
连接mysql并列出数据库中的表
.
替换THML标签
.
MVC循环产生数据
.
鼠标指向时变成手型
.
根据HTTP请求获取请求的json报文
.
按照插入顺序临时排序
.
使用对象字面量(literal)来定义属性
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3