turnPage.ascx.cs:
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class UserControls_turnPage : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int TotalNum; // 一共有多少条数据
public int PageSize; // 每页显示多少条数据
public string PageName; //转发页面的名称
public int TotalPage; // 总共页数
public string PageUrl; //转发路径
public int PageGroup; //
public void PageS(int pSize, int tNum, string pName, int pGroup)
{
this.PageSize = pSize;
this.TotalNum = tNum;
this.PageName = pName;
this.PageGroup = pGroup;
if ((TotalNum % PageSize) == 0)
{
TotalPage = TotalNum / PageSize;
}
else
{
TotalPage = TotalNum / PageSize + 1;
}
if (PageName.IndexOf("?") > 0)
{
PageUrl = PageName + "&";
}
else
{
PageUrl = PageName + "?";
}
}
public string Installize(int CurPage)
{
int pstart = 0; //起始页
int plast = 0; //终点页
bool pri = false; //上一页
bool next = true; //下一页
int sx = CurPage + 1;
int sxn = CurPage - 1;
string strpri = "<a href=" + PageUrl + "Page=" + sxn.ToString() + "><FONT color=#2821A8>上一页</font></a> ";
string strnext = "<a href=" + PageUrl + "Page=" + sx.ToString() + "><FONT color=#2821A8>下一页</font></a> ";
//确定起始页与终点页
if (CurPage == 1)
{
if (TotalPage <= PageGroup)
{
pstart = 1;
plast = TotalPage;
}
else
{
pstart = 1;
plast = PageGroup;
}
}
else
{
if (CurPage <= PageGroup + 1)
{
pstart = 1;
}
else
{
pstart = CurPage - PageGroup;
}
if (CurPage <= TotalPage + 1 - PageGroup)
{
plast = CurPage + PageGroup - 1;
}
else
{
plast = TotalPage;
next = false;
}
pri = true;
}
if (pri == false)
{
strpri = "";
}
if (next == false)
{
strnext = "";
}
string g = "";
for (int i = pstart; i <= plast; i++)
{
if (CurPage == i)
{
g += i.ToString() + " ";
}
else
{
g += "<a href=" + PageUrl + "Page=" + i.ToString() + "><font color=#2821A8>【" + i.ToString() + "】</font></a> ";
}
}
return strpri + g + strnext;
}
}
UsingPage.aspx:
<div style="width:100%; text-align:center">
<uc1:turnPage ID="turnPage1" runat="server" />
<asp:Label ID="lbNumber" runat="server"></asp:Label>
</div>
UsingPage.aspx.cs:
if (Request.QueryString["keyword"] != null)
{
keyword = Request.QueryString["keyword"].ToString();
}
if (!Page.IsPostBack)
{
BindPageData(keyword);
int numberCount = Int32.Parse(ViewState["numberCount"].ToString());
if (Request.QueryString["Page"] != null && numberCount > 0)
{
int page = Int32.Parse(Request.QueryString["Page"].ToString());
gvinvite.PageIndex = page - 1;
BindPageData(keyword);
this.turnPage1.PageS(4, numberCount, "UsingPage?keyword=" + HttpUtility.UrlEncode(keyword), 5);
this.lbNumber.Text = this.turnPage1.Installize(Int32.Parse(Request.QueryString["Page"]));
}
else
{
this.turnPage1.PageS(4, Int32.Parse(ViewState["numberCount"].ToString()), "InvitePostSearch.aspx?keyword=" + HttpUtility.UrlEncode(keyword), 5);
this.lbNumber.Text = this.turnPage1.Installize(1);
}
}