Code for pagination of Asp.net article content
Last Update:2018-12-08
Source: Internet
Author: User
Aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "articlepage. aspx. cs" inherits = "articlepage" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Article Paging </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Table Style = "text-align: center;" border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
<TD align = "Left" style = "width: 400px; Background-color: # ffff99;">
<Table Style = "width: 100%">
<Tr>
<TD style = "width: 100%; border-top: # ff3300 2px dotted; border-left-width: 2px; border-left-color: # ff3300; border-bottom: # ff3300 2px dotted; border-right-width: 2px; border-right-color: # ff3300; Height: 25px; text-align: center; ">
<% = Articletitle %> </TD>
</Tr>
</Table>
</TD>
</Tr>
<Tr>
<TD style = "width: 400px; Height: 100%;" align = "Left">
<P style = "background-color: oldlace">
<% = Article %>
</P>
</TD>
</Tr>
<Tr>
<TD style = "width: 400px; Background-color: # ffff99; Height: 48px;">
<Table Style = "width: 100%">
<Tr>
<TD style = "width: 100%; border-top: # ff3300 2px dotted; border-left-width: 2px; border-left-color: # ff3300; border-bottom: # ff3300 2px dotted; border-right-width: 2px; border-right-color: # ff3300; "align =" Left ">
<P>
<Asp: hyperlink id = "firstlink" runat = "server" visible = "false"> homepage </ASP: hyperlink>
<Asp: hyperlink id = "prelink" runat = "server" visible = "false"> previous page </ASP: hyperlink>
<Asp: hyperlink id = "nextlink" runat = "server" visible = "false"> next page </ASP: hyperlink>
<Asp: hyperlink id = "lastlink" runat = "server" visible = "false"> last page </ASP: hyperlink>
</P>
<P>
<%
If (pagesum> 1)
{
For (INT I = 1; I <= pagesum; I ++)
{
If (pageno = I)
{
%>
<% = I %>
<%
}
Else
{
%>
<A href = "? Page = <% = I %> "> <% = I %> </a>
<%
}
}
}
%>
Page number: <% = pageno %>/<% = pagesum %>
</P> </TD>
</Tr>
</Table>
</TD>
</Tr>
</Table>
<Br/>
</Div>
</Form>
</Body>
</Html>
// ==================================
Aspx. CS:
(C #)
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class articlepage: system. Web. UI. Page
{
Protected string article = "", articletitle = "";
Protected int pageno = 1, pagesum = 1;
Protected void page_load (Object sender, eventargs E)
{
// In actual application, the data here is obtained by operating the database
Articletitle = "article title ";
String filename = "20091795819.html ";
String mpath = server. mappath ("h3g /");
String filepath = mpath + filename;
Showarticle (filepath );
}
Protected void showarticle (string filepath)
{
String page = request. Params ["page"];
Int perpageline = 5; // number of rows per page
Arraylist Al = fileopr. readfilecontenttoarraylist (filepath); // read files empty into the array by row
Int contentline = Al. count;
Pagesum = (INT) system. Math. Ceiling (double) contentline/perpageline); // the total number of pages, rounded to 1
If (page = NULL | PAGE = "" | PAGE = "1 ")
{
Pageno = 1;
If (contentline <= perpageline)
{
For (INT I = 0; I <contentline; I ++)
{
Article + = al [I]. tostring ();
}
}
Else
{
For (INT I = 0; I <perpageline; I ++)
{
Article + = al [I]. tostring ();
}
Firstlink. Visible = false;
Prelink. Visible = false;
Nextlink. navigateurl = "? Page = "+ (pageno + 1 );
Nextlink. Visible = true;
Lastlink. navigateurl = "? Page = "+ pagesum;
Lastlink. Visible = true;
}
}
Else
{
Pageno = int. parse (PAGE );
If (pageno <pagesum)
{
For (INT I = perpageline * (pageno-1); I <perpageline * pageno; I ++)
{
Article + = al [I]. tostring ();
}
Firstlink. navigateurl = "? Page = 1 ";
Firstlink. Visible = true;
Prelink. navigateurl = "? Page = "+ (pageno-1 );
Prelink. Visible = true;
Nextlink. navigateurl = "? Page = "+ (pageno + 1 );
Nextlink. Visible = true;
Lastlink. navigateurl = "? Page = "+ pagesum;
Lastlink. Visible = true;
}
Else
{
For (INT I = contentline-perpageline * (pagesum-1); I <contentline; I ++)
{
Article + = al [I]. tostring ();
}
Firstlink. navigateurl = "? Page = 1 ";
Firstlink. Visible = true;
Prelink. navigateurl = "? Page = "+ (pageno-1 );
Prelink. Visible = true;
Nextlink. Visible = false;
Lastlink. Visible = false;
}
}
}
}
Reuse fileopr. CS:
Method In fileopr. readfilecontenttoarraylist (filepath:
Public static arraylist readfilecontenttoarraylist (string filepath)
{
Arraylist Al = new arraylist ();
Filestream FS = new filestream (filepath, filemode. Open, fileaccess. Read );
Streamreader SRD = new streamreader (FS, encoding. Default );
// Use the streamreader class to read files
SRD. basestream. Seek (0, seekorigin. Begin );
String strline = SRD. Readline ();
While (strline! = NULL)
{
Strline = SRD. Readline ();
Al. Add (strline + "\ n ");
}
// Close this streamreader object
SRD. Close ();
FS. Dispose ();
FS. Close ();
Return al;
}
Note: The idea of paging an article is to use the method of intercepting the number of characters in the text. In this way, when the content of the article is html Code If yes, it will cause typographical problems after paging.
The above Code is based on the number of rows. This method is better. When you manage document content files in the background, make sure that HTML code is well formatted with line breaks.