分页导航区域算法代码
分页代码
CS页面要定义几个 protected 变量,以便在HTML中能访问到
Code
<td id="tbPageIndex" runat="server">
<%
//不是第一页时,显示 上一页
if (pageIndex > 1)
{
%>
<a href="javascript:SetPageNavigation('<%=pageIndex - 1 %>');" style="color:Maroon;"><asp:Label ID="lblPrev" runat="server" Text="Prev"></asp:Label></a>
<%
}
int iStart = 0; //显示的起始页号;
int iEnd = 0; //显示的终止页号;
if (pageCount <= 10)
{
iStart = 1;
iEnd = pageCount;
}
else if (pageIndex == 1)
{
iStart = 1;
if (pageCount > 10)
iEnd = iStart + 9;
else
iEnd = pageCount;
}
else if (pageIndex <= 6)
{
iStart = pageIndex - 1;
if (pageCount - iStart < 9)
iEnd = pageCount;
else
iEnd = iStart + 9;
}
else if (pageCount - pageIndex < 4)
{
if (pageCount - pageIndex > 0)
iEnd = pageIndex + 1;
else
iEnd = pageCount;
if (iEnd - 9 <= 0)
iStart = 1;
else
iStart = iEnd - 9;
}
else
{
iStart = pageIndex - 5;
iEnd = pageIndex + 4;
}
for(int i=iStart;i<=iEnd;i++)
{
if( i == pageIndex)
{ Response.Write("<strong>[" + i +"]</strong> ");}
else
{
%>
<a href="javascript:SetPageNavigation('<%=i%>');" style="color:Maroon;"><%=i%></a>
<%
}
}
if (pageIndex < pageCount)
{
%>
<a href="javascript:SetPageNavigation('<%=pageIndex + 1 %>');" style="color:Maroon;"><asp:Label ID="lblNext" Text="Next" runat="server"></asp:Label></a>
<%
}
%>
<span style="padding-left:20px; color:Purple;"><%=pageIndex%>/<%=pageCount%></span>
</td>
<td id="tbPageIndex" runat="server">
<%
//不是第一页时,显示 上一页
if (pageIndex > 1)
{
%>
<a href="javascript:SetPageNavigation('<%=pageIndex - 1 %>');" style="color:Maroon;"><asp:Label ID="lblPrev" runat="server" Text="Prev"></asp:Label></a>
<%
}
int iStart = 0; //显示的起始页号;
int iEnd = 0; //显示的终止页号;
if (pageCount <= 10)
{
iStart = 1;
iEnd = pageCount;
}
else if (pageIndex == 1)
{
iStart = 1;
if (pageCount > 10)
iEnd = iStart + 9;
else
iEnd = pageCount;
}
else if (pageIndex <= 6)
{
iStart = pageIndex - 1;
if (pageCount - iStart < 9)
iEnd = pageCount;
else
iEnd = iStart + 9;
}
else if (pageCount - pageIndex < 4)
{
if (pageCount - pageIndex > 0)
iEnd = pageIndex + 1;
else
iEnd = pageCount;
if (iEnd - 9 <= 0)
iStart = 1;
else
iStart = iEnd - 9;
}
else
{
iStart = pageIndex - 5;
iEnd = pageIndex + 4;
}
for(int i=iStart;i<=iEnd;i++)
{
if( i == pageIndex)
{ Response.Write("<strong>[" + i +"]</strong> ");}
else
{
%>
<a href="javascript:SetPageNavigation('<%=i%>');" style="color:Maroon;"><%=i%></a>
<%
}
}
if (pageIndex < pageCount)
{
%>
<a href="javascript:SetPageNavigation('<%=pageIndex + 1 %>');" style="color:Maroon;"><asp:Label ID="lblNext" Text="Next" runat="server"></asp:Label></a>
<%
}
%>
<span style="padding-left:20px; color:Purple;"><%=pageIndex%>/<%=pageCount%></span>
</td>