asp.net 文章分页显示实现代码
投稿:mdxy-dxy
asp.net 文章分页显示实现代码,不多说看代码,简单,自己请适当修改
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
string str = "逝者的安息。";
Label1.Text = OutputBySize(str);
}
public string OutputBySize(string p_strContent)//分页函数
{
string m_strRet = "";
int m_intPageSize = 200;//文章每页大小
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=?ps=" + i + ">[" + i + "]</a> ";
}
if (m_intCurrentPage > 1)
m_strPageInfo = "<a href=?ps=" + (m_intCurrentPage - 1) + ">上一页</a>" + m_strPageInfo;
if (m_intCurrentPage < m_intTotalPage)
m_strPageInfo += "<a href=?ps=" + (m_intCurrentPage + 1) + ">下一页</a>";
//输出显示各个页码
this.ShowPageNumber.Text = "<p></p>" + m_strPageInfo;
}
else
{
m_strRet += p_strContent;
}
return m_strRet;
}