页面代码:
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="RepeaterPagingSortingDelete.aspx.cs"Inherits="RepeaterPagingSortingDelete"%>
-
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headrunat="server">
- <title>RepeaterPagingSortingDelete</title>
- <scriptlanguage="javascript"type="text/javascript">
- functionchooseAll(sender)
- {
- varinputs=document.all.tags("INPUT");
- //遍历页面上所有的input
- for(vari=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();
- }
- }
- }
- functionhighLightSelected(chkSelect)
- {
- if(chkSelect.checked)
- chkSelect.parentElement.parentElement.style.backgroundColor='#CCCCFF';
- else
- chkSelect.parentElement.parentElement.style.backgroundColor='white';
- }
- </script>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- <asp:LinkButtonid="lbtnFirst"runat="server"CommandName="First"OnCommand="lbtnPage_Command">|<</asp:LinkButton>
- <asp:LinkButtonid="lbtnPrevious"runat="server"CommandName="Previous"OnCommand="lbtnPage_Command"><<</asp:LinkButton>
- <asp:Labelid="lblMessage"runat="server"/>
- <asp:LinkButtonid="lbtnNext"runat="server"CommandName="Next"OnCommand="lbtnPage_Command">>></asp:LinkButton>
- <asp:LinkButtonid="lbtnLast"runat="server"CommandName="Last"OnCommand="lbtnPage_Command">>|</asp:LinkButton>
- 转到第<asp:DropDownListID="dropPage"runat="server"AutoPostBack="True"OnSelectedIndexChanged="dropPage_SelectedIndexChanged"></asp:DropDownList>页
- <asp:RepeaterID="rptProducts"runat="server"
- onitemcreated="rptProducts_ItemCreated"
- onitemdatabound="rptProducts_ItemDataBound">
- <HeaderTemplate>
- <tableborder="1">
- <tr>
- <tdid="tdAll"runat="server"><asp:CheckBoxID="chkAll"runat="server"/></td>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- <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>
- </tr>
- </HeaderTemplate>
- <ItemTemplate>
- <tr>
- <tdid="tdSelect"runat="server"><asp:CheckBoxID="chkSelect"runat="server"/></td>
- <td><asp:LabelID="lblProductID"runat="server"Text='<%#Eval("ProductID")%>'></asp:Label></td>
- <td><asp:LabelID="lblProductName"runat="server"Text='<%#Eval("ProductName")%>'></asp:Label></td>
- <td><asp:LabelID="lblSupplierID"runat="server"Text='<%#Eval("SupplierID")%>'></asp:Label></td>
- <td><asp:LabelID="lblCategoryID"runat="server"Text='<%#Eval("CategoryID")%>'></asp:Label></td>
- <td><asp:LabelID="lblQuantityPerUnit"runat="server"Text='<%#Eval("QuantityPerUnit")%>'></asp:Label></td>
- <td><asp:LabelID="lblUnitPrice"runat="server"Text='<%#Eval("UnitPrice")%>'></asp:Label></td>
- <td><asp:LabelID="lblUnitsInStock"runat="server"Text='<%#Eval("UnitsInStock")%>'></asp:Label></td>
- <td><asp:LabelID="lblUnitsOnOrder"runat="server"Text='<%#Eval("UnitsOnOrder")%>'></asp:Label></td>
- <td><asp:LabelID="lblReorderLevel"runat="server"Text='<%#Eval("ReorderLevel")%>'></asp:Label></td>
- <td><asp:CheckBoxID="chkDiscontinued"runat="server"Checked='<%#Eval("Discontinued")%>'Enabled="false"/></td>
- </tr>
- </ItemTemplate>
- <FooterTemplate>
- </table>
- </FooterTemplate>
- </asp:Repeater>
- <asp:ButtonID="btnDelete"runat="server"Text="删除"onclick="btnDelete_Click"/>
- </div>
- </form>
- </body>
- </html>
-
后台代码:
- usingSystem;
- usingSystem.Collections;
- usingSystem.Configuration;
- usingSystem.Data;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Xml.Linq;
- usingSystem.Data.SqlClient;
-
- publicpartialclassRepeaterPagingSortingDelete:System.Web.UI.Page
- {
- //每页显示的最多记录的条数
- privateintpageSize=10;
- //当前页号
- privateintcurrentPageNumber;
- //排序表达式
- privatestringsortExpression=string.Empty;
- //排序方向
- privatestringsortDirection=string.Empty;
- //显示数据的总条数
- privatestaticintrowCount;
- //总页数
- privatestaticintpageCount;
-
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- if(!IsPostBack)
- {
- SqlConnectioncn=newSqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
- SqlCommandcmd=newSqlCommand("GetProductsCount",cn);
- cmd.CommandType=CommandType.StoredProcedure;
- cn.Open();
- rowCount=(int)cmd.ExecuteScalar();
- cn.Close();
- pageCount=(rowCount-1)/pageSize+1;
- currentPageNumber=1;
- for(inti=1;i<=pageCount;i++)
- {
- dropPage.Items.Add(newListItem(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"]);
- }
-
- privatevoidQuery()
- {
- SetButton(currentPageNumber);
- SqlConnectioncn=newSqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
- SqlCommandcmd=newSqlCommand("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();
- }
-
- protectedvoidlbtnPage_Command(objectsender,CommandEventArgse)
- {
- 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();
- }
-
- protectedvoidlbtnSort_Command(objectsender,CommandEventArgse)
- {
- if(e.CommandName!=ViewState["sortExpression"].ToString())
- {
- sortDirection="ASC";
- }
- else
- {
- if(sortDirection=="ASC")
- {
- sortDirection="DESC";
- }
- elseif(sortDirection=="DESC"||sortDirection==string.Empty)
- {
- sortDirection="ASC";
- }
- }
- sortExpression=e.CommandName;
- Query();
- }
-
- protectedvoiddropPage_SelectedIndexChanged(objectsender,EventArgse)
- {
- currentPageNumber=int.Parse(dropPage.SelectedValue);
- Query();
- }
-
- privatevoidSetButton(intcurrentPageNumber)
- {
- lbtnFirst.Enabled=currentPageNumber!=1;
- lbtnPrevious.Enabled=currentPageNumber!=1;
- lbtnNext.Enabled=currentPageNumber!=pageCount;
- lbtnLast.Enabled=currentPageNumber!=pageCount;
- }
-
- privatevoidSave()
- {
- ViewState["currentPageNumber"]=currentPageNumber;
- ViewState["sortExpression"]=sortExpression;
- ViewState["sortDirection"]=sortDirection;
- }
-
- protectedvoidrptProducts_ItemCreated(objectsender,RepeaterItemEventArgse)
- {
- if(e.Item.ItemType==ListItemType.Header)
- {
- if(!string.IsNullOrEmpty(sortDirection))
- {
- LabellblSort=newLabel();
- lblSort.EnableTheming=false;
- lblSort.Font.Name="webdings";
- lblSort.Font.Size=FontUnit.Small;
- lblSort.Text=sortDirection=="ASC"?"5":"6";
- (e.Item.FindControl("td"+sortExpression)asHtmlTableCell).Controls.Add(lblSort);
- }
- }
- }
- protectedvoidbtnDelete_Click(objectsender,EventArgse)
- {
- CheckBoxchkSelect;
- LabellblProductID;
- foreach(RepeaterItemiteminrptProducts.Items)
- {
- chkSelect=item.FindControl("chkSelect")asCheckBox;
- if(chkSelect!=null&&chkSelect.Checked)
- {
- lblProductID=item.FindControl("lblProductID")asLabel;
- //Response.Write(lblProductID.Text);
- //可以在这里完成删除逻辑
- }
- }
- }
-
- protectedvoidrptProducts_ItemDataBound(objectsender,RepeaterItemEventArgse)
- {
- if(e.Item.ItemType==ListItemType.Header)
- {
- CheckBoxchkAll=e.Item.FindControl("chkAll")asCheckBox;
- chkAll.Attributes.Add("onclick","chooseAll('"+chkAll.ClientID+"')");
- }
-
- if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
- {
- CheckBoxchkSelect=e.Item.FindControl("chkSelect")asCheckBox;
- chkSelect.Attributes.Add("onclick","highLightSelected(this);");
- }
- }
- }
-