简体   繁体   English

如何在 ASP.NET 中对单行数据进行分页

[英]How to do paging a single row data in ASP.NET

I have an article which is very large (single database row) and I need to show page by page.我有一篇非常大的文章(单个数据库行),我需要逐页显示。

If it would have been a dataset (multiple row) then I could have shown on a Grid with server side paging.如果它是一个数据集(多行),那么我可以在带有服务器端分页的网格上显示。

For a single large row, how to fragment it page by page?对于单个大行,如何逐页分片?

Typical case when a user submits a very large article.用户提交非常大的文章的典型情况。

Can you split the text content of the article on paragraph breaks and make a List<string> out of them, then bind that to your Paging and Grid controls.您能否将文章的文本内容拆分为分段符并从中创建一个List<string> ,然后将其绑定到您的 Paging 和 Grid 控件。 Then when the page is posted back, the article will be split into chunks in the same way and paging will work.然后当页面回传时,文章将以相同的方式分成块,并且分页将起作用。

You could of course cache the List<string> across requests to save having to go to the database and process the record repeatedly.您当然可以跨请求缓存List<string>以将 go 保存到数据库并重复处理记录。

How about:怎么样:

  • Pull the data into a string[] by .Split ing on some average size of lines通过.Split在一些平均大小的行上将数据拉入string[]
  • Add each item into a DataRow将每个项目添加到 DataRow
  • Assign the .DataSource to the table.DataSource分配给表

And then enjoy the paging provided by the GridView .然后享受GridView提供的分页。

Here is how you can LINQ into the string[] , and bind the result with grid. 这是您如何LINQ 放入string[]并将结果与网格绑定的方法。

There's no general solution.没有通用的解决方案。 One way would be to store it in database already divided by pages.一种方法是将其存储在已按页面划分的数据库中。 But I think It would be better to split article to pages during reading it from database.但我认为在从数据库中读取文章时将文章拆分为页面会更好。 For example:例如:

var article = new ArticleRepository().GetById(1);
//and here article has Pages property that splits original article whatever You like
datagrig.datasource = article.Pages;

You can change the logic in article and Pages property when You decide to split articles in different ways.当您决定以不同方式拆分文章时,您可以更改文章和页面属性中的逻辑。 And it won't affect ASP.NET page.它不会影响 ASP.NET 页面。 Maybe later You decide that page should have info on what was on previous or what is on next page.也许稍后您决定该页面应该包含有关上一页或下一页内容的信息。 Then Page could have two properties: PreviousPageExcerpt, NextPageExcerpt.那么 Page 可以有两个属性:PreviousPageExcerpt、NextPageExcerpt。 It's better than converting article to array of strings.这比将文章转换为字符串数组要好。 Having all the logic in Article class also let's You Unit Test dividing articles to pages.拥有文章 class 中的所有逻辑后,您还可以对文章进行分页单元测试。

Maybe some thing like that (I didn't test it.)也许是这样的(我没有测试过。)

    Public Function SplitBySize(ByVal strInput As String, ByVal iSize As Integer) As String()
        Dim strA() As String
        Dim iLength As Integer = strInput.Length()
        Dim iWords As Integer = iLength / iSize + IIf((iLength Mod iSize  0), 1, 0)
        ReDim strA(iWords)
        Dim j As Integer = 0, i As Integer
        For i = 0 To iLength Step iSize
            strA(j) = Mid(strInput, i + 1, iSize)
            j = j + 1
        Next i
        Return strA
    End Function

    Sub Page_Load()

        Dim id As Integer = 5
        Dim page As Integer = 2
        Dim chrsCountPerPage As Integer = 1000

        Dim topic As String = "" '(From s In topics Where s.id = id Select s).first().details

        Dim STopic = SplitBySize(topic, chrsCountPerPage)

        If page > STopic.Length Then
            page = 1
        End If

        lblTopic.text = STopic(page - 1)

    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM