文章,记录按内容分页显示,根据文章内容按字数进行分页

先从数据库中检索出要文章,(很长的文章)
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> private string OutputArticle()
{
// OnlyoutputoneRecordofthe"AriticID"is2
string m_strRet = "" ;
。。。。
//检索数据库文章的代码。
return m_strRet;
}

对文章进行分页:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> public string OutputBySize( string p_strContent)
{
int m_intPageSize = 1000 ; // 文章每页大小
int m_intCurrentPage = 1 ; // 设置第一页为初始页
int m_intTotalPage = 0 ;
int m_intArticlelength = p_strContent.Length; // 文章长度
if (m_intPageSize < m_intArticlelength)
{
// 如果每页大小大于文章长度时就不用分页了
if (m_intArticlelength % m_intPageSize == 0 )
{
// settotalpagescount
m_intTotalPage = m_intArticlelength / m_intPageSize;
}
else
{
// ifthetotalsize
m_intTotalPage = m_intArticlelength / m_intPageSize + 1 ;
}
if (Request.QueryString[ " pages " ] != null )
{
// setCurrentpagenumber
try
{
// 处理不正常的地址栏的值
m_intCurrentPage = Convert.ToInt32(Request.QueryString[ " pages " ]);
if (m_intCurrentPage > m_intTotalPage)
m_intCurrentPage
= m_intTotalPage;
}
catch
{
m_intCurrentPage
= m_intCurrentPage;
}
}
// setthepagecontent设置获取当前页的大小
m_intPageSize = m_intCurrentPage < m_intTotalPage ? m_intPageSize:(m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1 ));
string m_strRet = p_strContent.Substring(m_intPageSize * (m_intCurrentPage - 1 ),m_intPageSize);
string m_strPageInfo = " <p></p> " ;
for ( int i = 1 ;i <= m_intTotalPage;i ++ )
{
if (i == m_intCurrentPage)
m_strPageInfo
+= " <b> " + i + " </b>| " ;
else
m_strPageInfo
+= " <ahref=viewArticle.aspx?pages= " + i + " > " + i + " </a>| " ;
}
// 输出显示各个页码
this .labPageNumber.Text = m_strPageInfo;
}
return m_strRet;
}

最后在页面加载事件中调用上面的方法:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> protected void Page_Load( object sender,EventArgse)
{
if ( ! Page.IsPostBack)
{
string m_strContent = OutputArticle();
this .labContent.Text = OutputBySize(m_strContent);
}
}
页面代码很简单为:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <% @PageLanguage = " C# " AutoEventWireup = " true " CodeFile = " ViewArticle.aspx.cs " Inherits = " ViewArticle " %>

<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > UntitledPage </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Label ID ="labContent" runat ="server" ></ asp:Label >
< br />
< br />
< asp:Label ID ="labPageNumber" runat ="server" ></ asp:Label ></ div >
</ form >
</ body >
</ html >

你可能感兴趣的:(html,XHTML,asp)