asp.net怎么实现分页啊 求代码指导!!!!

2024年12月01日 12:54
有3个网友回答
网友(1):

那得看需求了分页有好多中的 有用存储过程来分页的 也可以直接用.net代码来实现
int CurrentPage = ToPage;
int PageSize = 5;
int PageCount;
string sqlStr = "select count(*) from wmv ";
int RecordCount = db.GetRecordCount(sqlStr);
BindGrid();
//* 取得记录总数,计算总页数
if ((RecordCount % PageSize) != 0)
{
PageCount = RecordCount / PageSize + 1;
}
else
{
PageCount = RecordCount / PageSize;
}
if (ToPage > PageCount)
{
CurrentPage = PageCount;
}
if (CurrentPage <= 1)
{
sqlStr = "select top " + PageSize + " wmv.[id],wmv.[name],wmv.[miaoxu],wmv.[userid],wmv.[lock],wmv.[hits],wmv.[img] from wmv";
}
else
{
sqlStr = "select top " + PageSize + " wmv.[id],wmv.[name],wmv.[miaoxu],wmv.[userid],wmv.[lock],wmv.[hits],wmv.[img] from wmv where wmv.[id] not in(select top " + PageSize * (CurrentPage - 1) + " wmv.[id] from wmv) ";
}

this.lbTotalPage.Text = Convert.ToString(PageCount);
this.hlkFirstPage.NavigateUrl = "?ToPage=1";
this.hlkLastPage.NavigateUrl = "?ToPage=" + PageCount;
this.lbCurrentPage.Text = Convert.ToString(CurrentPage);
if (CurrentPage <= 1)
{
this.hlkPrevPage.Enabled = false;
this.hlkFirstPage.Enabled = false;
CurrentPage = 1;
}
else
{
this.hlkPrevPage.Enabled = true;
this.hlkFirstPage.Enabled = true;
this.hlkPrevPage.NavigateUrl = "?ToPage=" + (ToPage - 1);
}
if (CurrentPage >= PageCount)
{
this.hlkNextPage.Enabled = false;
this.hlkLastPage.Enabled = false;
CurrentPage = PageCount;
}
else
{
this.hlkNextPage.Enabled = true;
this.hlkLastPage.Enabled = true;
this.hlkNextPage.NavigateUrl = "?ToPage=" + (ToPage + 1);
}
DataTable dt = db.GetDataTable(sqlStr);
if (dt.Rows.Count < 1)
{
Literal l1 = new Literal();
l1.ID = "myl1";
l1.Text = "暂无数据!";
Repeater1.Controls.Add(l1);
}
else
{
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
int yushu = CurrentPage / 10;
string pageTxt = "";
for (int i = 0; i < 10; i++)
{
int numInt = yushu * 10 + i + 1;
if (numInt < PageCount + 1)
{
if (numInt == CurrentPage)
{
pageTxt += "" + numInt + " ";
}
else
{
pageTxt += "" + numInt + " ";
}
}
}
this.pageNums.Text = pageTxt.ToString();
this.pageing.Visible = true;
}
}

网友(2):

控件就是代码封装到一起而已。方便,易重用。

网友(3):

你是用什么数据绑定的控件的呢?