在很多时候我们对一些文章很长时,往往浏览时用鼠标滑来滑去非常麻烦。要是我们对这篇文章划分为几个
页面,用不同的面页去浏览不但看起来很爽,而且效率方面也是大的提高了。
下面是一段代码,给大家做个参考:
先从数据库中检索出要文章,(很长的文章)
对文章进行分页:
最后在页面加载事件中调用上面的方法:
页面,用不同的面页去浏览不但看起来很爽,而且效率方面也是大的提高了。
下面是一段代码,给大家做个参考:
先从数据库中检索出要文章,(很长的文章)
private string OutputArticle()
{//Only output one Record of the "AriticID" is 2
string m_strRet = "";
。。。。
//检索数据库文章的代码。
return m_strRet;
}
{//Only output one Record of the "AriticID" is 2
string m_strRet = "";
。。。。
//检索数据库文章的代码。
return m_strRet;
}
对文章进行分页:
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)
{//set total pages count
m_intTotalPage = m_intArticlelength / m_intPageSize;
}
else
{//if the totalsize
m_intTotalPage = m_intArticlelength / m_intPageSize + 1;
}
if (Request.QueryString["pages"] != null)
{//set Current page number
try
{//处理不正常的地址栏的值
m_intCurrentPage = Convert.ToInt32(Request.QueryString["pages"]);
if (m_intCurrentPage > m_intTotalPage)
m_intCurrentPage = m_intTotalPage;
}
catch
{
m_intCurrentPage = m_intCurrentPage;
}
}
//set the page content 设置获取当前页的大小
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 += "<a href=viewArticle.aspx?pages=" + i + ">" + i + "</a>|";
}
//输出显示各个页码
this.labPageNumber.Text = m_strPageInfo;
}
return m_strRet;
}
{
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)
{//set total pages count
m_intTotalPage = m_intArticlelength / m_intPageSize;
}
else
{//if the totalsize
m_intTotalPage = m_intArticlelength / m_intPageSize + 1;
}
if (Request.QueryString["pages"] != null)
{//set Current page number
try
{//处理不正常的地址栏的值
m_intCurrentPage = Convert.ToInt32(Request.QueryString["pages"]);
if (m_intCurrentPage > m_intTotalPage)
m_intCurrentPage = m_intTotalPage;
}
catch
{
m_intCurrentPage = m_intCurrentPage;
}
}
//set the page content 设置获取当前页的大小
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 += "<a href=viewArticle.aspx?pages=" + i + ">" + i + "</a>|";
}
//输出显示各个页码
this.labPageNumber.Text = m_strPageInfo;
}
return m_strRet;
}
最后在页面加载事件中调用上面的方法:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string m_strContent = OutputArticle();
this.labContent.Text = OutputBySize(m_strContent);
}
}
页面代码很简单为:{
if (!Page.IsPostBack)
{
string m_strContent = OutputArticle();
this.labContent.Text = OutputBySize(m_strContent);
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewArticle.aspx.cs" Inherits="ViewArticle" %>
<!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>Untitled Page</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>
一个简单的按内容分页就是这样了。简单吧。<!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>Untitled Page</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>