c#获得伪静态页码
时间:2011-05-06 来源:落冰
/// <summary>
/// 获得页码显示链接
/// </summary>
/// <param name="curPage">当前页数</param>
/// <param name="countPage">总页数</param>
/// <param name="url">超级链接地址</param>
/// <param name="extendPage">周边页码显示个数上限</param>
/// <returns>页码html</returns>
public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage)
{
return GetPageNumbers(curPage, countPage, url, extendPage, "page");
}
/// <summary>
/// 获得页码显示链接
/// </summary>
/// <param name="curPage">当前页数</param>
/// <param name="countPage">总页数</param>
/// <param name="url">超级链接地址</param>
/// <param name="extendPage">周边页码显示个数上限</param>
/// <param name="pagetag">页码标记</param>
/// <returns>页码html</returns>
public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage, string pagetag)
{
return GetPageNumbers(curPage, countPage, url, extendPage, pagetag, null);
}
/// <summary>
/// 获得页码显示链接
/// </summary>
/// <param name="curPage">当前页数</param>
/// <param name="countPage">总页数</param>
/// <param name="url">超级链接地址</param>
/// <param name="extendPage">周边页码显示个数上限</param>
/// <param name="pagetag">页码标记</param>
/// <param name="anchor">锚点</param>
/// <returns>页码html</returns>
public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage, string pagetag, string anchor)
{
if (pagetag == "")
pagetag = "page";
int startPage = 1;
int endPage = 1;
if (url.IndexOf("?") > 0)
url = url + "&";
else
url = url + "?";
string t1 = "<a href=\"" + url + "&" + pagetag + "=1";
string t2 = "<a href=\"" + url + "&" + pagetag + "=" + countPage;
if (anchor != null)
{
t1 += anchor;
t2 += anchor;
}
t1 += "\">«</a>";
t2 += "\">»</a>";
if (countPage < 1)
countPage = 1;
if (extendPage < 3)
extendPage = 2;
if (countPage > extendPage)
{
if (curPage - (extendPage / 2) > 0)
{
if (curPage + (extendPage / 2) < countPage)
{
startPage = curPage - (extendPage / 2);
endPage = startPage + extendPage - 1;
}
else
{
endPage = countPage;
startPage = endPage - extendPage + 1;
t2 = "";
}
}
else
{
endPage = extendPage;
t1 = "";
}
}
else
{
startPage = 1;
endPage = countPage;
t1 = "";
t2 = "";
}
StringBuilder s = new StringBuilder("");
s.Append(t1);
for (int i = startPage; i <= endPage; i++)
{
if (i == curPage)
{
s.Append("<span>");
s.Append(i);
s.Append("</span>");
}
else
{
s.Append("<a href=\"");
s.Append(url);
s.Append(pagetag);
s.Append("=");
s.Append(i);
if (anchor != null)
{
s.Append(anchor);
}
s.Append("\">");
s.Append(i);
s.Append("</a>");
}
}
s.Append(t2);
return s.ToString();
}
/// <summary>
/// 判断文件流是否为UTF8字符集
/// </summary>
/// <param name="sbInputStream">文件流</param>
/// <returns>判断结果</returns>
private static bool IsUTF8(FileStream sbInputStream)
{
int i;
byte cOctets; // octets to go in this UTF-8 encoded character
byte chr;
bool bAllAscii = true;
long iLen = sbInputStream.Length;
cOctets = 0;
for (i = 0; i < iLen; i++)
{
chr = (byte)sbInputStream.ReadByte();
if ((chr & 0x80) != 0) bAllAscii = false;
if (cOctets == 0)
{
if (chr >= 0x80)
{
do
{
chr <<= 1;
cOctets++;
}
while ((chr & 0x80) != 0);
cOctets--;
if (cOctets == 0)
return false;
}
}
else
{
if ((chr & 0xC0) != 0x80)
return false;
cOctets--;
}
}
if (cOctets > 0)
return false;
if (bAllAscii)
return false;
return true;
}
/// <summary>
/// 获得伪静态页码显示链接
/// </summary>
/// <param name="curPage">当前页数</param>
/// <param name="countPage">总页数</param>
/// <param name="url">超级链接地址</param>
/// <param name="extendPage">周边页码显示个数上限</param>
/// <returns>页码html</returns>
public static string GetStaticPageNumbers(int curPage, int countPage, string url, string expname, int extendPage)
{
return GetStaticPageNumbers(curPage, countPage, url, expname, extendPage, 0);
}
/// <summary>
/// 获得伪静态页码显示链接
/// </summary>
/// <param name="curPage">当前页数</param>
/// <param name="countPage">总页数</param>
/// <param name="url">超级链接地址</param>
/// <param name="extendPage">周边页码显示个数上限</param>
/// <param name="forumrewrite">当前版块是否使用URL重写</param>
/// <returns>页码html</returns>
public static string GetStaticPageNumbers(int curPage, int countPage, string url, string expname, int extendPage, int forumrewrite)
{
int startPage = 1;
int endPage = 1;
string t1 = "<a href=\"" + url + "-1" + expname + "\">«</a>";
string t2 = "<a href=\"" + url + "-" + countPage + expname + "\">»</a>";
if (forumrewrite == 1)
{
t1 = "<a href=\"" + url + "/1/list" + expname + "\">«</a>";
t2 = "<a href=\"" + url + "/" + countPage + "/list" + expname + "\">»</a>";
}
if (forumrewrite == 2)
{
t1 = "<a href=\"" + url + "/\">«</a>";
t2 = "<a href=\"" + url + "/" + countPage + "/\">»</a>";
}
if (countPage < 1) countPage = 1;
if (extendPage < 3) extendPage = 2;
if (countPage > extendPage)
{
if (curPage - (extendPage / 2) > 0)
{
if (curPage + (extendPage / 2) < countPage)
{
startPage = curPage - (extendPage / 2);
endPage = startPage + extendPage - 1;
}
else
{
endPage = countPage;
startPage = endPage - extendPage + 1;
t2 = "";
}
}
else
{
endPage = extendPage;
t1 = "";
}
}
else
{
startPage = 1;
endPage = countPage;
t1 = "";
t2 = "";
}
StringBuilder s = new StringBuilder("");
s.Append(t1);
for (int i = startPage; i <= endPage; i++)
{
if (i == curPage)
{
s.Append("<span>");
s.Append(i);
s.Append("</span>");
}
else
{
s.Append("<a href=\"");
if (forumrewrite == 1)
{
s.Append(url);
if (i != 1)
{
s.Append("/");
s.Append(i);
}
s.Append("/list");
s.Append(expname);
}
else if (forumrewrite == 2)
{
s.Append(url);
s.Append("/");
if (i != 1)
{
s.Append(i);
s.Append("/");
}
}
else
{
s.Append(url);
if (i != 1)
{
s.Append("-");
s.Append(i);
}
s.Append(expname);
}
s.Append("\">");
s.Append(i);
s.Append("</a>");
}
}
s.Append(t2);
return s.ToString();
}
/// <summary>
/// 获得帖子的伪静态页码显示链接
/// </summary>
/// <param name="expname"></param>
/// <param name="countPage">总页数</param>
/// <param name="url">超级链接地址</param>
/// <param name="extendPage">周边页码显示个数上限</param>
/// <returns>页码html</returns>
public static string GetPostPageNumbers(int countPage, string url, string expname, int extendPage)
{
int startPage = 1;
int endPage = 1;
int curPage = 1;
string t1 = "<a href=\"" + url + "-1" + expname + "\">«</a>";
string t2 = "<a href=\"" + url + "-" + countPage + expname + "\">»</a>";
if (countPage < 1) countPage = 1;
if (extendPage < 3) extendPage = 2;
if (countPage > extendPage)
{
if (curPage - (extendPage / 2) > 0)
{
if (curPage + (extendPage / 2) < countPage)
{
startPage = curPage - (extendPage / 2);
endPage = startPage + extendPage - 1;
}
else
{
endPage = countPage;
startPage = endPage - extendPage + 1;
t2 = "";
}
}
else
{
endPage = extendPage;
t1 = "";
}
}
else
{
startPage = 1;
endPage = countPage;
t1 = "";
t2 = "";
}
StringBuilder s = new StringBuilder("");
s.Append(t1);
for (int i = startPage; i <= endPage; i++)
{
s.Append("<a href=\"");
s.Append(url);
s.Append("-");
s.Append(i);
s.Append(expname);
s.Append("\">");
s.Append(i);
s.Append("</a>");
}
s.Append(t2);
return s.ToString();
}
相关阅读 更多 +