Paging long articles in ASP.NET using C#

4.58 (12 votes)

Long articles are better broken into bite-sized chunks over several pages. With static HTML, this is easily achieved by dividing the article into logical separations and creating separate .htm files for each. Here's how to do it using C# for an article that gets posted to a database.

The Regular Expression Split() method returns a one-dimensional zero-based array containing a number of substrings, so it is perfect for this job. What I want to do is take an article (which is a string) and divide it into substrings. In order to do this, I need a delimiter, and I use <!--pagebreak-->. As I enter the article into the database, I place <!--pagebreak--> at the points I want to break the article.

Then, having extracted the article from the database, I pass it to the following static method which I have in a utility class:

public static string PageArticle(string Article)
{
  string Output;
  string ThisPage = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
	
  string PageNo;
  PageNo = HttpContext.Current.Request.QueryString["Page"];

  if (Article.IndexOf("<!--pagebreak-->") != -1)
  {
    string[] Pages = Regex.Split(Article, "<!--pagebreak-->");
    int TotalPages = Pages.GetUpperBound(0) + 1;
    int Counter = 1;
    int PageNumber = 1;
    if (PageNo != null)
    {
      PageNumber = System.Convert.ToInt32(PageNo);
    }
      Output = Pages[PageNumber - 1];
      Output += "<p>Go to Page ";
      while (Counter <= TotalPages)
      {
       if (Counter == PageNumber)
        {
           Output += Counter.ToString() + " ";
        }
         else
          {
            Output += "<a href=\"" + ThisPage + "?Page=" + Counter.ToString();
            Output += "\">" + Counter.ToString() + "</a> ";
          }
          Counter++;
      }
      Output += "</p>";
    }
    else
	{
      Output = Article;
    }
  return Output;
}

The method gets the current page using the HttpContext class, and then checks to see if the article being passed to it contains the delimiter. If so, the Regular Expression Split() method breaks the article into its constituent parts. The method then checks the current page's querystirng to see if there is a page number available. If not, it sets the current page to 1, as this must be the first page, and shows the first element of the array. If there is a page number, then it shows the element from the array that matches the page number.

The method then iterates from 1 to the upper bound of the array to produce the links that will provide navigation through the article. If the number being written is the same as the current page, no hyperlink is provided, otherwise the HttpContext class providesthe current file name via the Request.ServerVariables["SCRIPT_NAME"] item, so the link will point to whichever page calls the method.

If the delimiter is not present in the body of the article, the article is returned complete.

I placed the method in a class called Utils, which is put in the App_Code folder, so it is easily called when databinding thus:

<%# Utils.PageArticle(Eval("Article")) %>

Date Posted:
Last Updated:
Posted by:
Total Views to date: 20439

3 Comments

- David

Hi Mike,

I pull out a long article from database table by way of query string parameter, and the url of the pulled out article is:
http://localhost:54516/TestPageBreak/ViewArticle.aspx?ArticleID=101

Follow your tutorial, I put this line in the ViewArticle.aspx:
<%#Utils.PageArticle(Eval("Article")) %>

I use a text editor to insert into database table this long article and mark pagebreaks by <!--pagebreak--> phrases, when pull out, no paging at all and no error message either.

Please guide me.

Thank you.

David

- David

Hi Mike,

This is me David who just send a comment several hours ago.

I finally got it to work! What's an experience! Thank you a bunch for your script that helps solve my long article paging problem.

Here is why it did not work before:
I use FCKeditor to input articles into database table. The FCKeditor turned my "<!--pagebreak-->" to "<!--pagebreak-->". That was why your great script detected no conventional page break mark! Now, I fix it using only --pagebreak-- and it works perfectly.

Thank you again.

Regards,

David

- hung

thanks very much

Recent Comments

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....