asp.net Repeater之非常好的数据分页

yizhihongxing

asp.net Repeater 控件是 .NET 框架提供的一种用于数据绑定的控件,它可以帮助我们轻松地将数据绑定到前端页面上。在使用 asp.net Repeater 控件时,如果需要对大量数据进行分页,我们通常可以通过自定义分页来实现。下面给出一个使用 asp.net Repeater 实现非常好的自定义分页的完整攻略,其中包含两个示例:

一、基本分页功能的实现

1. 实现基本的分页控制

要实现基本的分页控制,我们首先需要定义一个分页控制器,它包含以下控件:

<div class="pagination">
    <asp:LinkButton ID="lnkPrev" runat="server" Text="&lt;" CommandArgument="Prev" OnCommand="Page_Changed"></asp:LinkButton>
    <asp:Repeater ID="rptPager" runat="server">
        <ItemTemplate>
            <asp:LinkButton ID="lnkPage" runat="server" CommandArgument='<%# Eval("PageIndex") %>' Text='<%# Eval("PageText") %>' OnClick="Page_Changed"></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>
    <asp:LinkButton ID="lnkNext" runat="server" Text="&gt;" CommandArgument="Next" OnCommand="Page_Changed"></asp:LinkButton>
</div>

其中,lnkPrevlnkNext 分别表示上一页和下一页的链接按钮,rptPager 表示分页导航条,它是一个 asp.net Repeater 控件。

2. 实现数据源绑定和分页数据筛选

对于数据源绑定和数据筛选,我们需要在后台代码中实现。首先,我们需要定义一个方法,用于分页查询数据库中的数据,并返回指定的数据集:

private void BindData(int pageIndex)
{
    string sql = "SELECT * FROM TableName ORDER BY OrderColumn";
    DataTable dt = DBHelper.ExecuteDataTable(sql);

    PagedDataSource pds = new PagedDataSource();
    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 10;
    pds.CurrentPageIndex = pageIndex;

    lnkPrev.Enabled = !pds.IsFirstPage;
    lnkNext.Enabled = !pds.IsLastPage;

    rptPager.DataSource = BuildPagerItems(pds.PageCount, pageIndex);
    rptPager.DataBind();
}

其中,BuildPagerItems() 方法可以根据当前页码和总页数生成一个分页项的集合,具体实现见下文。

然后,我们需要在 Page_Changed 事件中响应分页事件,根据分页控制器的状态更新数据源:

protected void Page_Changed(object sender, CommandEventArgs e)
{
    int pageIndex = int.Parse(e.CommandArgument.ToString());

    if (e.CommandArgument.ToString() == "Next")
    {
        pageIndex = CurrentPageIndex + 1;
    }
    else if (e.CommandArgument.ToString() == "Prev")
    {
        pageIndex = CurrentPageIndex - 1;
    }

    CurrentPageIndex = pageIndex;
    BindData(CurrentPageIndex);
}

最后,我们需要在前端页面中定义一些辅助页面控件,用来记录当前页码、总页数等信息:

<asp:HiddenField ID="hdnCurrentPage" runat="server" />
<asp:HiddenField ID="hdnTotalPages" runat="server" />

值得注意的是,我们在 BindData() 方法中通过计算数据集中的总页数并将其存储到隐藏域控件中,以供后续使用。

3. 构建分页项集合

要构建分页项集合,我们需要定义一个 BuildPagerItems() 方法,该方法根据当前页码和总页数生成一个分页项的集合,代码如下:

private List<PagerItem> BuildPagerItems(int pageCount, int currentPageIndex)
{
    List<PagerItem> items = new List<PagerItem>();

    PagerItem item = new PagerItem();
    item.PageIndex = 0;
    item.PageText = "首页";
    items.Add(item);

    item = new PagerItem();
    item.PageIndex = currentPageIndex - 1;
    item.PageText = "&lt;";
    items.Add(item);

    int start = 1;
    int end = pageCount;
    if (pageCount > 10)
    {
        start = Math.Max(currentPageIndex - 5, 1);
        end = Math.Min(currentPageIndex + 4, pageCount);
        if (end - start < 10)
        {
            start = end - 9;
        }
    }

    for (int i = start; i <= end; i++)
    {
        item = new PagerItem();
        item.PageIndex = i - 1;
        item.PageText = i.ToString();
        if (i == currentPageIndex + 1)
        {
            item.CssClass = "active";
        }
        items.Add(item);
    }

    item = new PagerItem();
    item.PageIndex = currentPageIndex + 1;
    item.PageText = "&gt;";
    items.Add(item);

    item = new PagerItem();
    item.PageIndex = pageCount - 1;
    item.PageText = "末页";
    items.Add(item);

    hdnCurrentPage.Value = currentPageIndex.ToString();
    hdnTotalPages.Value = pageCount.ToString();

    return items;
}

二、带有搜索栏的分页功能实现

1. 定义一个带有搜索栏的页面

要实现带有搜索栏的分页,我们需要在网页上方添加一个搜索栏,它包含一个文本框和一个搜索按钮:

<div class="search-bar">
    <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="BtnSearch_Click" />
</div>

2. 在后台代码中实现数据按需绑定

为了实现按需绑定,我们需要在 BtnSearch_Click 事件中根据关键词执行查询操作,然后对查询结果进行分页:

protected void BtnSearch_Click(object sender, EventArgs e)
{
    string sql = "SELECT * FROM TableName WHERE ColumnName LIKE '%" + txtSearch.Text + "%' ORDER BY OrderColumn";
    DataTable dt = DBHelper.ExecuteDataTable(sql);

    PagedDataSource pds = new PagedDataSource();
    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 10;
    pds.CurrentPageIndex = 0;

    CurrentPageIndex = 0;
    BindData(pds);
}

值得注意的是,我们在 BindData() 方法中传入了 PagedDataSource 对象,而不是直接传入页码,从而实现按需绑定的功能。

3. 更新分页项集合

在按需绑定的过程中,我们可能需要更新分页项集合,以反映新的总页数和当前页码。为此,我们可以在 BindData() 方法中添加一些适当的代码来实现:

hdnCurrentPage.Value = "0";
hdnTotalPages.Value = pds.PageCount.ToString();
rptPager.DataSource = BuildPagerItems(pds.PageCount, 0);
rptPager.DataBind();

示例代码

最后,附上示例代码:

private int CurrentPageIndex
{
    get { return int.Parse(hdnCurrentPage.Value); }
    set { hdnCurrentPage.Value = value.ToString(); }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData(CurrentPageIndex);
    }
}

private void BindData(int pageIndex)
{
    string sql = "SELECT * FROM TableName ORDER BY OrderColumn";
    DataTable dt = DBHelper.ExecuteDataTable(sql);

    PagedDataSource pds = new PagedDataSource();
    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 10;
    pds.CurrentPageIndex = pageIndex;

    lnkPrev.Enabled = !pds.IsFirstPage;
    lnkNext.Enabled = !pds.IsLastPage;

    rptPager.DataSource = BuildPagerItems(pds.PageCount, pageIndex);
    rptPager.DataBind();
}

protected void Page_Changed(object sender, CommandEventArgs e)
{
    int pageIndex = int.Parse(e.CommandArgument.ToString());

    if (e.CommandArgument.ToString() == "Next")
    {
        pageIndex = CurrentPageIndex + 1;
    }
    else if (e.CommandArgument.ToString() == "Prev")
    {
        pageIndex = CurrentPageIndex - 1;
    }

    CurrentPageIndex = pageIndex;
    BindData(CurrentPageIndex);
}

private List<PagerItem> BuildPagerItems(int pageCount, int currentPageIndex)
{
    List<PagerItem> items = new List<PagerItem>();

    PagerItem item = new PagerItem();
    item.PageIndex = 0;
    item.PageText = "首页";
    items.Add(item);

    item = new PagerItem();
    item.PageIndex = currentPageIndex - 1;
    item.PageText = "&lt;";
    items.Add(item);

    int start = 1;
    int end = pageCount;
    if (pageCount > 10)
    {
        start = Math.Max(currentPageIndex - 5, 1);
        end = Math.Min(currentPageIndex + 4, pageCount);
        if (end - start < 10)
        {
            start = end - 9;
        }
    }

    for (int i = start; i <= end; i++)
    {
        item = new PagerItem();
        item.PageIndex = i - 1;
        item.PageText = i.ToString();
        if (i == currentPageIndex + 1)
        {
            item.CssClass = "active";
        }
        items.Add(item);
    }

    item = new PagerItem();
    item.PageIndex = currentPageIndex + 1;
    item.PageText = "&gt;";
    items.Add(item);

    item = new PagerItem();
    item.PageIndex = pageCount - 1;
    item.PageText = "末页";
    items.Add(item);

    hdnCurrentPage.Value = currentPageIndex.ToString();
    hdnTotalPages.Value = pageCount.ToString();

    return items;
}

protected void BtnSearch_Click(object sender, EventArgs e)
{
    string sql = "SELECT * FROM TableName WHERE ColumnName LIKE '%" + txtSearch.Text + "%' ORDER BY OrderColumn";
    DataTable dt = DBHelper.ExecuteDataTable(sql);

    PagedDataSource pds = new PagedDataSource();
    pds.DataSource = dt.DefaultView;
    pds.AllowPaging = true;
    pds.PageSize = 10;
    pds.CurrentPageIndex = 0;

    CurrentPageIndex = 0;
    BindData(pds);

    hdnCurrentPage.Value = "0";
    hdnTotalPages.Value = pds.PageCount.ToString();
    rptPager.DataSource = BuildPagerItems(pds.PageCount, 0);
    rptPager.DataBind();
}

注意事项:

  1. PagerItem 类是一个自定义的数据结构,用于表示分页项的数据,其中包含了 PageIndexPageTextCssClass 三个属性;
  2. hdnCurrentPagehdnTotalPages 是两个用于记录当前页码和总页数的隐藏域控件;
  3. 实际应用中,我们需要将 DBHelper.ExecuteDataTable() 替换为自己的数据库访问操作代码;
  4. 由于本示例中并没有实现样式,因此需要在 buildPagerItems() 方法中添加样式逻辑,以调整分页项显示效果。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:asp.net Repeater之非常好的数据分页 - Python技术站

(1)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • C#文件目录操作方法汇总

    C#文件目录操作方法汇总 在C#编程中,文件和目录操作是非常常见的需求。本文总结了常用的C#文件目录操作方法,包括路径操作、目录创建、文件创建、文件读写、文件复制、文件删除等多个方面,旨在帮助读者快速实现对文件和目录的操作。 路径操作 获取当前应用程序执行文件所在目录 string path = AppDomain.CurrentDomain.BaseDir…

    C# 2023年6月7日
    00
  • C#利用反射来判断对象是否包含某个属性的实现方法

    可以通过反射来动态获取和设置对象的属性值。在C#中,可以使用反射判断某个对象是否包含某个特定属性。下面是使用反射来判断对象是否包含某个属性的实现方法: 1.获取类型对象 使用反射,首先需要获取代表要分析的类型的Type对象。可以通过以下两种方法实现: 使用类型名字符串: Type type = Type.GetType(“命名空间.类名”); 直接通过类类型…

    C# 2023年6月1日
    00
  • c#高效率导出多维表头excel的实例代码

    c#高效率导出多维表头excel的实例代码 介绍 在实际开发过程中,我们常常遇到需要将数据导出到excel的场景。而有些情况下,导出的excel中可能会有多维表头,这时候我们需要一种高效的方法来实现这个功能。本文将介绍一种使用C#语言实现高效率导出多维表头Excel的实例代码。 准备工作 在该实例的实现中,我们需要使用到两个第三方库,分别是EPPlus和Cl…

    C# 2023年5月15日
    00
  • IIS7.5中调试.Net 4.0网站出现无厘头、500错误的解决方法

    在IIS7.5中调试.Net4.0网站时,有时会出现无厘头、500错误的情况。这可能是由于IIS7.5没有正确配置.Net4.0应用程序池而导致的。本文将提供解决方案,帮助解决这个问题。 问题描述 在IIS7.5中调试.Net4.0网站时,有时会出现无厘头、500错误的情况。具体表现为,网站无法正常运行,或者在使用某些功能时崩溃。 解决方案 方法一:配置.N…

    C# 2023年5月15日
    00
  • C# 多网卡 Server Listen

    当服务器有多个网卡时,我们需要指定所有网卡进行监听,以确保能够接收所有连接请求。下面是实现 C# 多网卡 Server Listen 的完整攻略。 第一步:获取本机所有 IP 地址 在 C# 中,可以使用 Dns.GetHostAddresses() 方法获取本机所有的 IP 地址,代码如下: var hostName = Dns.GetHostName()…

    C# 2023年6月6日
    00
  • 详解VS2017 Linux 上.NET Core调试

    详解VS2017 Linux 上.NET Core调试 在本攻略中,我们将详细介绍如何使用Visual Studio 2017在Linux上调试.NET Core应用程序。我们将介绍如何配置调试环境、如何在Visual Studio中设置调试器,并提供两个示例说明。 配置调试环境 在将.NET Core应用程序调试到Linux上之前,需要进行以下准备工作: …

    C# 2023年5月16日
    00
  • 简单聊聊c# 事件

    好的。首先,我将介绍C#中的事件。事件是一种声明,当特定动作发生时,事件会在代码中触发。例如,可以在单击按钮时触发单击事件,或者在完全加载页面时触发加载事件。在事件被触发时,可以调用与事件相关的代码,以提供所需的功能。 在C#中,我们可以通过声明delegate类型来定义事件。delegate类型是一种数据类型,它引用一个方法,该方法可以用于处理事件的调用。…

    C# 2023年6月1日
    00
  • C# GetHashCode():获取此实例的哈希代码

    C#中的GetHashCode()方法用于获取对象的哈希码(HashCode)。哈希码是一种用于快速识别对象的整数。哈希码的计算方法是将对象中各种不同类型的值转换成32位整数。通常,我们使用哈希表来操作对象。哈希表将哈希码作为索引,通过哈希码查找对象,这样可以非常快速地定位到对象。 以下是使用GetHashCode()方法的一些示例: 示例1: class …

    C# 2023年4月19日
    00
合作推广
合作推广
分享本页
返回顶部