文章分页浏览(二)
分页的方法:
View Code
public string OutputBySize(string p_strContent, string bType)
{
string m_strRet = "";
int m_intPageSize =500;//文章每页大小
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["ps"] != null)
{//set Current page number
try
{//处理不正常的地址栏的值
m_intCurrentPage = Convert.ToInt32(Request.QueryString["ps"]);
if (m_intCurrentPage > m_intTotalPage)
m_intCurrentPage = m_intTotalPage;
}
catch
{
//m_intCurrentPage = m_intCurrentPage;
}
}
//set the page content 设置获取当前页的大小
if (m_intCurrentPage < m_intTotalPage)
{
m_intPageSize = m_intCurrentPage < m_intTotalPage ? m_intPageSize : (m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1));
m_strRet += p_strContent.Substring(m_intPageSize * (m_intCurrentPage - 1), m_intPageSize);
}
else if (m_intCurrentPage == m_intTotalPage)
{
int mm_intPageSize = m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1);
m_strRet += p_strContent.Substring(m_intArticlelength - mm_intPageSize);
}
string m_strPageInfo = "";
for (int i = 1; i <= m_intTotalPage; i++)
{
if (i == m_intCurrentPage)
m_strPageInfo += "[" + i + "]";
else
m_strPageInfo += " <a href=gywm.aspx?Btype=" + bType + "&ps=" + i + ">[" + i + "] </a> ";
}
if (m_intCurrentPage > 1)
m_strPageInfo = " <a href=gywm.aspx?Btype=" + bType + "&ps=" + (m_intCurrentPage - 1) + ">上一页 </a>" + m_strPageInfo;
if (m_intCurrentPage < m_intTotalPage)
m_strPageInfo += " <a href=gywm.aspx?Btype=" + bType + "&ps=" + (m_intCurrentPage + 1) + ">下一页 </a>";
//输出显示各个页码
this.ShowPageNumber.Text = " <p> </p>" + m_strPageInfo;
}
else
{
m_strRet += p_strContent;
}
return m_strRet;
}
{
string m_strRet = "";
int m_intPageSize =500;//文章每页大小
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["ps"] != null)
{//set Current page number
try
{//处理不正常的地址栏的值
m_intCurrentPage = Convert.ToInt32(Request.QueryString["ps"]);
if (m_intCurrentPage > m_intTotalPage)
m_intCurrentPage = m_intTotalPage;
}
catch
{
//m_intCurrentPage = m_intCurrentPage;
}
}
//set the page content 设置获取当前页的大小
if (m_intCurrentPage < m_intTotalPage)
{
m_intPageSize = m_intCurrentPage < m_intTotalPage ? m_intPageSize : (m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1));
m_strRet += p_strContent.Substring(m_intPageSize * (m_intCurrentPage - 1), m_intPageSize);
}
else if (m_intCurrentPage == m_intTotalPage)
{
int mm_intPageSize = m_intArticlelength - m_intPageSize * (m_intCurrentPage - 1);
m_strRet += p_strContent.Substring(m_intArticlelength - mm_intPageSize);
}
string m_strPageInfo = "";
for (int i = 1; i <= m_intTotalPage; i++)
{
if (i == m_intCurrentPage)
m_strPageInfo += "[" + i + "]";
else
m_strPageInfo += " <a href=gywm.aspx?Btype=" + bType + "&ps=" + i + ">[" + i + "] </a> ";
}
if (m_intCurrentPage > 1)
m_strPageInfo = " <a href=gywm.aspx?Btype=" + bType + "&ps=" + (m_intCurrentPage - 1) + ">上一页 </a>" + m_strPageInfo;
if (m_intCurrentPage < m_intTotalPage)
m_strPageInfo += " <a href=gywm.aspx?Btype=" + bType + "&ps=" + (m_intCurrentPage + 1) + ">下一页 </a>";
//输出显示各个页码
this.ShowPageNumber.Text = " <p> </p>" + m_strPageInfo;
}
else
{
m_strRet += p_strContent;
}
return m_strRet;
}
多思考,多创新,才是正道!