Repeater 使用 AspNetPager分页

本文详细介绍了如何使用AspNetPager在ASP.NET项目中实现分页功能,包括数据库连接、查询记录数、分页数据填充及页面跳转的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Repeater 使用 AspNetPager分页,在网上查资料用了我很长时间,但觉得都不太好而且用到自己的项目中都有错误,这个是转的别人发表的

 DB.cs文件代码
 using System;
using System.Data;
using System.Configuration;
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.Data;
using System.Data.SqlClient;

/// <summary>
///DB 的摘要说明
/// </summary>
public class DB
{
 public DB()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    public static SqlConnection creaton()
    {
        SqlConnection con = null;
        try
        {
            con = new SqlConnection("server=limingfei;database=limingfei;uid=sa;pwd=sa;");
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return con;
    }
}
 
cs文件下代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            SqlConnection conn = DB.creaton();
            conn.Open();
            SqlCommand Count = new SqlCommand();
            Count.Connection = conn;
            Count.CommandText = "select count(*) from [user]";
            AspNetPager1.RecordCount = (int)Count.ExecuteScalar();
            conn.Close();
            BindData();
        }

    }

    public void BindData()
    {
        SqlConnection conn = DB.creaton();
        string sql = "select * from [user] order by ID desc";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "10");
        DataTable dt = ds.Tables["10"];
       Repeater1.DataSource =dt;
        Repeater1.DataBind();
       
    }
    protected void AspNetPager1_PageChanged1(object sender, EventArgs e)
    {
        BindData();
    }
}
aspx文件代码:
 <%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!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>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:limingfeiConnectionString %>"
        SelectCommand="SELECT [userName], [category], [releaseName], [PicAddress], [neirong] FROM [user]">
    </asp:SqlDataSource>
   
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate><center>

                <table width="1002" border="1" >
                    <tr>
                        <td width="150px" height="93px">
                            <img src='<%# eval_r("PicAddress")%>' alt="哈哈全是美女呀!!!" width="150px" height="93px" />
                        </td>
                        <td width="802px">
                            <table>
                                <tr>
                                    <td>
                                        名称:<asp:Label ID="Label1" runat="server" Text='<%# eval_r("userName")%>' />
                                        类别:<asp:Label ID="Label2" runat="server" Text='<%# eval_r("category")%>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        内容:<asp:Label ID="Label3" runat="server" Text='<%# eval_r("neirong")%>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        发布者:<asp:Label ID="Label4" runat="server" Text='<%# eval_r("releaseName")%>' />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>
        <table width="1002" border="1">
            <td>
                <webdiyer:AspNetPager ID="AspNetPager1" runat="server" CurrentPageButtonPosition="Center"
                    FirstPageText="首页" LastPageText="尾页" NextPageText="后页" PrevPageText="前页" OnPageChanged="AspNetPager1_PageChanged1">
                </webdiyer:AspNetPager>
            </td>
        </table>
        </center>

    </form>
   
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值