Repeater控件分页效果(嵌套checkBox进行删除)

前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="right.aspx.cs" Inherits="XZC_零件管理系统.rights" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
body {
    margin-left: 3px;
    margin-top: 0px;
    margin-right: 3px;
    margin-bottom: 0px;
}
.STYLE1 {
    color: #e1e2e3;
    font-size: 12px;
}
.STYLE6 {color: #000000; font-size: 12; }
.STYLE10 {color: #000000; font-size: 12px; }
.STYLE19 {
    color: #344b50;
    font-size: 12px;
}
.STYLE21 {
    font-size: 12px;
    color: #3b6375;
}
.STYLE22 {
    font-size: 12px;
    color: #295568;
}
-->
</style>
</head>

<body>
    <form id="form1" runat="server">
<table width="100%" border="1" cellpadding="0" cellspacing="0" style="1006px;border-collapse:collapse; text-align:center;">
  <tr>
    <td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="24" bgcolor="#353c44"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="19" valign="bottom">&nbsp;</td>
                </tr>
            </table></td>
            </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td>
        <asp:Repeater ID="Repeater1" runat="server" >
            <HeaderTemplate>
                <table style="100%">
                    <tr>
                        <td style=" 50px; height: 20px">请选择</td>
                        <td style=" 30px; height: 20px">UserID</td>
                        <td style=" 30px; height: 20px">UserName</td>
                        <td style=" 30px; height: 20px">LoginName</td>
                        <td style=" 30px; height: 20px">Password</td>
                        <td style=" 30px; height: 20px">Email</td>
                        <td style=" 30px; height: 20px">Power</td>
                    </tr>
            </HeaderTemplate>

            <ItemTemplate>
                <tr>
                    <td><asp:CheckBox ID="CheckBox1" runat="server" />
                        <asp:Label ID="lblID" Text='<%#Eval( "UserID")%>' runat="server"></asp:Label></td>
                    <td><%# Eval("UserID") %></td>
                    <td><%# Eval("UserName") %></td>
                    <td><%# Eval("LoginName") %></td>
                    <td><%# Eval("Password") %></td>
                    <td><%# Eval("Email") %></td>
                    <td><%# Eval("Power") %></td>
                </tr>
            </ItemTemplate>

            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
                 <div style="background-color:#dedede; 1006px;">
                   <asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" />
                   <asp:label ID="lblCurrentPage" runat="server"></asp:label>
                   <asp:HyperLink id="lnkFrist" runat="server">首页</asp:HyperLink>
                   <asp:HyperLink id="lnkPrev" runat="server">上一页</asp:HyperLink>
                   <asp:HyperLink id="lnkNext" runat="server">下一页</asp:HyperLink> 
                   <asp:HyperLink id="lnkEnd" runat="server">尾页</asp:HyperLink>
                </div>
      </td>
  </tr>
  <tr>
    <td height="30"></td>
  </tr>
</table>
    </form>
</body>
</html>
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;

namespace XZC_零件管理系统
{
    public partial class rights : System.Web.UI.Page
    {
        DataTable dt = new DataTable();
        PagedDataSource pds = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                BindData();
            }
        }

        public void BindData()
        {
            int TotalCount = 0;//总记录数
            int TotalPage = 1; //总页数
            int CurPage;       //当前页
            string Sqlstr = "  select * from People";

            dt = DbHelperSQL.Query(Sqlstr).Tables[0];
            pds.DataSource = dt.DefaultView;
          
            TotalCount = pds.Count;
            pds.AllowPaging = true;
            pds.PageSize = 20;

           
            if (Request.QueryString["Page"] != null)
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            else
                CurPage = 1;

            if (TotalCount == 0)
                TotalPage = 1;
            else
            {
                if (TotalCount % pds.PageSize == 0)
                    TotalPage = TotalCount / pds.PageSize;
                else
                    TotalPage = TotalCount / pds.PageSize + 1;
            }

            pds.CurrentPageIndex = CurPage - 1;
            lblCurrentPage.Text = "" + TotalCount.ToString() + "条记录 当前页:" + CurPage.ToString() + "/" + TotalPage;

            lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            if (!pds.IsFirstPage)
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

            if (!pds.IsLastPage)
                lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + TotalPage;

            Repeater1.DataSource = pds;
            Repeater1.DataBind();

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string ID = "";

            for (int i = 0; i < this.Repeater1.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)this.Repeater1.Items[i].FindControl("CheckBox1");
                if (cbox.Checked == true)
                {
                    if (ID == "")
                    {
                        ID = "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";
                    }
                    else
                    {
                        ID += "," + "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";
                    }
                }
            }
            string  strsql = "delete from People where UserID in (" + ID + ")";
            try
            {
                DbHelperSQL.ExecuteSql(strsql);
                System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('刪除成功!');</script>");
            }
            catch (System.Data.SqlClient.SqlException E)
            {
                throw new Exception(E.Message);
            }
            this.BindData();

        }
    }
}
原文地址:https://www.cnblogs.com/anbylau2130/p/2845667.html