开发者

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

开发者 https://www.devze.com 2023-03-17 01:25 出处:网络
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开发者_StackOverflow a Grid with

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开发者_StackOverflow 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. 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.


How about:

  • Pull the data into a string[] by .Spliting on some average size of lines
  • Add each item into a DataRow
  • Assign the .DataSource to the table

And then enjoy the paging provided by the GridView.

Here is how you can LINQ into the string[], and bind the result with grid.


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. 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. 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消