页面有如下控件:有1个检索框,能对表中的某个字段进行检索。
1个gridview,gridview上面和下面各有4个button按钮,分别是首页、上一页、下一页、末页。
当打开页面的时候,显示表中所有的数据。
点击翻页按钮可以对 gridview翻页。
当在检索框中输入值对某个字段检索之后,显示表中符合条件的数据。
点击翻页按钮也可以对gridview翻页。
请问大家,该怎么做?
1个gridview,gridview上面和下面各有4个button按钮,分别是首页、上一页、下一页、末页。
当打开页面的时候,显示表中所有的数据。
点击翻页按钮可以对 gridview翻页。
当在检索框中输入值对某个字段检索之后,显示表中符合条件的数据。
点击翻页按钮也可以对gridview翻页。
请问大家,该怎么做?
1.设置GridView的AllowPaging的属性为true,
2.设置首页、上一页、下一页、末页,在GridView的PagerSettings中有很多选项!!
在pageindexchaning事件里重新绑定一次
www.datasonar.com
{
LinkButton btn = (LinkButton)sender;
switch (btn.CommandName)
{
case "First":
PageIndex = 0;
break;
case "Prev"://if( PageIndex > 0 )
PageIndex = PageIndex - 1;
break;
case "Next"://if( PageIndex < PageCount -1)
PageIndex = PageIndex + 1;
break;
case "Last":
PageIndex = PageCount - 1;
break;
}
GridViewDataBind();
}
GVPara.CurrentPageIndex = e.NewPageIndex;
GridViewDataBind();
这样就实现翻页的功能了。
把条件检索和显示所有数据都写到GridViewDataBind()里了?
{
DataSet ds = GetCustomersData();
recordCount = ds.Tables[0].Rows.Count;
//获取当前的页数
pageCount = (int)Math.Ceiling(recordCount * 1.0 / PageSize);
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
if (recordCount == 0)
{
this.DataGrid1.CurrentPageIndex = 0;
}
else if (this.DataGrid1.CurrentPageIndex >= pageCount)
{
this.DataGrid1.CurrentPageIndex = pageCount - 1;
}
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
NavigationStateChange();
}
public static DataSet GetCustomersData()
{
SqlConnection conn = new SqlConnection(connString);
string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";
SqlCommand comm = new SqlCommand(sqlStr, conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
if(!IsPostback)
{
BindData();
}
private void BindData()
{
//查询数据。在通过按钮分页,还可使用aspnetpager分页控件
}
参考
参考
ding 很少使用自带的分页,存储过程分页还可以,要多少条取多少条,google 一下,