gridview分頁


PagedDataSource ObjectPage = new PagedDataSource();//创建分页类
ObjectPage.DataSource = ds.Tables["Table"].DefaultView;//设置数据源
ObjectPage.AllowPaging = true;//设置分页
ObjectPage.PageSize = 4;
if (Request.QueryString["Page"] != null)
{
DangQianPage = Convert.ToInt32(Request.QueryString["Page"]);
DangQianPage = Math.Min(DangQianPage, ObjectPage.PageCount);
DangQianPage = Math.Max(DangQianPage, 1);
}
else
{
DangQianPage = 1;
}
ObjectPage.CurrentPageIndex = DangQianPage - 1;//取得当前页码
ZhongPage = ObjectPage.PageCount;
MRNum = ObjectPage.DataSourceCount;
MeiYiPage = ObjectPage.Count;
lblCurPage.Text = "第" + DangQianPage.ToString() + "/" + ZhongPage.ToString() + "页";
lblTnum.Text = "共:" + MRNum.ToString() + "条记录";
lblEachPage.Text = "每页有: " + MeiYiPage.ToString() + " 条记录";
if (ObjectPage.CurrentPageIndex != 0)
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?categoryid=" + strID + "&Page=" + Convert.ToString(1);
if (ObjectPage.CurrentPageIndex != ZhongPage - 1)
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?categoryid=" + strID + "&Page=" + Convert.ToString(ZhongPage);
if (!ObjectPage.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?categoryid=" + strID + "&Page=" + Convert.ToString(DangQianPage - 1);
if (!ObjectPage.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?categoryid=" + strID + "&Page=" + Convert.ToString(DangQianPage + 1);
GridView1.DataSource = ObjectPage;
GridView1.DataBind();
(2)实现跳转指定页的“GO”按钮事件代码如下:
protected void btnPage_Click(object sender, EventArgs e)
{
int MyPageNum = 0;
string strID = "0";
if (!txtPage.Text.Equals(""))
MyPageNum = Convert.ToInt32(Request.Form["txtPage"]);
if (MyPageNum <= 0 || MyPageNum > ZhongPage)
Response.Redirect(Request.CurrentExecutionFilePath + "?categoryid=" + strID + "&Page=" + Convert.ToString(1));
else
Response.Redirect(Request.CurrentExecutionFilePath + "?categoryid=" + strID + "&Page=" + Convert.ToString(MyPageNum));
}
说明:可以将上述方法封装在一个类文件中,以便更加方便地调用

張貼留言

0 留言