定制分页随着中继器和SQL [英] Customized Paging With Repeater And SQL

查看:144
本文介绍了定制分页随着中继器和SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个很好的教程教我如何做一个简单的数据绑定控件像中继器来实现高性能的寻呼功能定制的寻呼控制。

I've been looking for a good tutorial to teach me how to make a customized Paging control with a simple DataBound control like Repeater to implement a high performance paging feature.

我发现了很多关于这个问题的文章,但其中非是一个完整的答案。

I've found a lot of articles about that issue, but non of them was a complete answer.

这一个是真棒,与统计和这样做的不同方法之间的比较大,但问题是它太旧之前的SQL Server有它的新功能ROW_NUMBER()

This one is awesome, with statistics and great comparison between the different methods of doing so, but the problem is it's too old before SQL Server had it's new function ROW_NUMBER()

<一个href=\"http://imar.spaanjaars.com/479/n-layered-web-applications-with-aspnet-35-part-4-sorting-paging-and-filtering\"相对=nofollow>的N-分层Web应用程序与
ASP.NET 3.5 4部分:排序,分页
和过滤的(数据库分页
部分而忽略其他)

这一次几乎是完美的,但他用网格的现有寻呼功能使用自定义的方法进行分页,而不是一个中继器。

This one is almost perfect, but he used the grid's existing paging feature with a customized Method for paging, instead of a repeater.

<一个href=\"http://www.dotnetfunda.com/articles/article456-custom-seo-friendly-paging-with-aspnet-repeater-or-datalist-control-.aspx\"相对=nofollow>自定义搜索引擎友好的与分页
ASP.NET直放站或DataList控件
控制

我觉得这个人是太复杂了,事情能完成的工作更容易

I think this one is WAY too complicated, and things can get done easier

我使用的是SQL,项目中继器(使用$ C $,没有使用的数据源C-后面直接绑定),页面编号中继器(其中将有一个链接作为一个ItemTemplate通过查询字符串这样的方法可以检索项目下一部分),一个标签来保存当前页码和标题。

I'm using SQL, Items Repeater (with direct binding in the code-behind with no datasources used), PageNumbers repeater (which will have a link as an ItemTemplate to pass querystring so the used method could retrieve the next portion of Items), A Label to hold the current page number and title.

我一直在试图实施<一个例子href=\"http://imar.spaanjaars.com/479/n-layered-web-applications-with-aspnet-35-part-4-sorting-paging-and-filtering\"相对=nofollow>的N-分层Web应用程序使用ASP.NET 3.5 4部分:排序,分页和过滤的(数据库分页科而忽略其他)的。到目前为止,我已经创建了我的数据访问Lyaer一个SQL命令看起来像这样:

I've been trying to implement the example on N-Layered Web Applications with ASP.NET 3.5 Part 4: Sorting, Paging and Filtering (The Database Paging Section and ignore the rest). So far I've created a SQL command in my Data Access Lyaer that looks like this :

WITH Records AS ( SELECT ItemId, ItemName, ROW_NUMBER() OVER (ORDER BY ItemId) AS 'RowNumber' FROM   Items)  SELECT * FROM Records WHERE (RowNumber BETWEEN (@startIndex) AND @startIndex + @pageSize - 1)

但现在我卡在如何在我的presentation层使用它!

but now I'm stuck on how to use it in my presentation layer!

推荐答案

您可以创建一个自定义的方法来呈现自己的分页控制。下面是一个例子:

You can create a custom method to render your own pagination control. Here is an example:

    /// <summary>
    /// Produces html for a pagination control.
    /// </summary>
    /// <param name="page">Page number for the current page (1-based index).</param>
    /// <param name="pageSize">Number or items per page.</param>
    /// <param name="totalItems">Total number of items across all pages.</param>
    /// <returns>Html of a pagination control.</returns>
    public string RenderPaginationControl(int page, int pageSize, int totalItems)
    {
        int totalPages = (int)Math.Ceiling((double)totalItems/pageSize);

        // Create pager.
        StringBuilder pagerSb = new StringBuilder();
        for (int i = 1; i <= totalPages; ++i)
        {
            // If it is NOT a link to current page.
            if (i != page) { pagerSb.Append(string.Format("<a href='/data.aspx?page={0}'>{0}</a>", i)); }
            // If it is the link to current page.
            else { pagerSb.Append(string.Format("<span>{0}</span>", i)); }
        }

        return pagerSb.ToString();
    }

你可以从你的SQL见之余,您还需要调用

As you can see apart from your sql, you will also need to call

SELECT COUNT(*) FROM Items

和值传递给的 TOTALITEMS 的在 RenderPaginationControl

和尽可能的结合直放站而言 - 它是pretty直截了当:

And as far as the binding to Repeater is concerned - it's pretty straight forward:

this.MyRepeater.DataSource = DAL.GetItems(page, pageSize);
this.MyRepeater.DataBind();

int totalItems = DAL.GetTotalNumberOfItems();
this.PaginationLabel.Text = RenderPaginationControl(page, pageSize, totalItems);

这篇关于定制分页随着中继器和SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆