public void BindData()
{
//读取数据函数返回DATASET,这个我就不写了。你自己写吧
DataTable dsTable = 你的函数名.Tables[0].DefaultView;
PagedDataSource objPag = new PagedDataSource();//添加分页
objPag.DataSource = dsTable;
objPag.AllowPaging = true;//允许分页
objPag.PageSize = 5; //分页大小
int curPage;//当前页
if (Request.QueryString["Page"] != null)
{
curPage = Int32.Parse(Request.QueryString["Page"]);
}
else
{
curPage = 1;
}
objPag.CurrentPageIndex = curPage - 1;//获取当前页的索引
this.Label1.Text = curPage.ToString();//获取第一页
this.Label2.Text = objPag.PageCount.ToString();//获取总的页
//如果当前也不是首页,设置前一页
if (!objPag.IsFirstPage)
{
HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);
}
//如果当前的也不是尾页,则设置后页的链接地址
if (!objPag.IsLastPage)
{
HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);
}
//首页的链接
HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
//尾页的链接
HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPag.PageCount.ToString();
DataList1.DataSource = objPag;
DataList1.DataBind();
}
{
//读取数据函数返回DATASET,这个我就不写了。你自己写吧
DataTable dsTable = 你的函数名.Tables[0].DefaultView;
PagedDataSource objPag = new PagedDataSource();//添加分页
objPag.DataSource = dsTable;
objPag.AllowPaging = true;//允许分页
objPag.PageSize = 5; //分页大小
int curPage;//当前页
if (Request.QueryString["Page"] != null)
{
curPage = Int32.Parse(Request.QueryString["Page"]);
}
else
{
curPage = 1;
}
objPag.CurrentPageIndex = curPage - 1;//获取当前页的索引
this.Label1.Text = curPage.ToString();//获取第一页
this.Label2.Text = objPag.PageCount.ToString();//获取总的页
//如果当前也不是首页,设置前一页
if (!objPag.IsFirstPage)
{
HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);
}
//如果当前的也不是尾页,则设置后页的链接地址
if (!objPag.IsLastPage)
{
HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);
}
//首页的链接
HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
//尾页的链接
HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPag.PageCount.ToString();
DataList1.DataSource = objPag;
DataList1.DataBind();
}
curPage = (curPage<0 ? 0 : (curPage>=objPag.PageCount ? objPag.PageCount-1 : curPage));
{
if (!this.IsPostBack)
{
this.BindGridView();
}
} //显示全部信息的视图,绑定
private void BindGridView()
{
try
{
DataView dv = new DataView(dboperate.selectAll());
dv.Sort = "bianhao DESC";
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
dv.Dispose(); this.LblCurrentIndex.Text = "当前页:" + Convert.ToString(this.GridView1.PageIndex + 1) + " ";
this.LblPageCount.Text = "总页数:" + this.GridView1.PageCount.ToString() + " ";
this.LblRecordCount.Text = "总记录数:" + dv.Table.DataSet.Tables["cuizpesctable"].Rows.Count.ToString() + " 条"; if (dv.Table.DataSet.Tables["cuizpesctable"].Rows.Count==0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false; LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false; LblNoRecord.Visible = true;
}
else if (this.GridView1.PageCount == 1)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "prev":
if (this.GridView1.PageIndex > 0)
{
this.GridView1.PageIndex -= 1;
}
break;
case "next":
if (this.GridView1.PageIndex < (this.GridView1.PageCount - 1))
{
this.GridView1.PageIndex += 1;
}
break;
case "last":
this.GridView1.PageIndex = (this.GridView1.PageCount - 1);
break;
default:
this.GridView1.PageIndex = 0;
break;
}
this.BindGridView();
}