任何人都可以告诉我如何使用datapager实现自定义分页。现在我已经设法使用2个按钮(用于下一页和上一页)和LoadListview()方法,但我想在datapager中使用此方法。有什么建议吗?
代码隐藏文件:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BusinessConnectionString"].ToString());
SqlDataAdapter adap;
int startIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadListview();
}
}
private void LoadListview()
{
string FindWhat = Request.QueryString["Find"];
string FindWhere = Request.QueryString["Where"];
string TownName = FindWhere;
string CountyName = FindWhere;
string PostcodeName = FindWhere;
//startIndex = int.Parse(ViewState["index"].ToString());
startIndex = MyDataPager.StartRowIndex * MyDataPager.PageSize;
int endIndex = startIndex + MyDataPager.PageSize;
adap = new SqlDataAdapter("SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY id_Business) AS MyRow, id_Business, Name, TownName FROM UbeloView WHERE (Name Like '%" + FindWhat + "%' AND TownName Like '%" + TownName + "%') UNION ALL SELECT ROW_NUMBER() OVER (ORDER BY id_Business) AS MyRow, id_Business, Name, TownName FROM UbeloView WHERE (Name Like '%" + FindWhat + "%' AND CountyName Like '%" + CountyName + "%') UNION ALL SELECT ROW_NUMBER() OVER (ORDER BY id_Business) AS MyRow, id_Business, Name, TownName FROM UbeloView WHERE (Name Like '%" + FindWhat + "%' AND PostcodeName Like '%" + PostcodeName + "%')) AS log WHERE MyRow >=" + startIndex + " AND MyRow <=" + endIndex + " ", conn);
DataSet ds = new DataSet();
adap.Fill(ds);
lstBusiness.DataSource = ds;
lstBusiness.DataBind();
}
protected void MyDataPager_PreRender(object sender, EventArgs e)
{
LoadListview();
}
答案 0 :(得分:0)
本文可以帮助您:http://www.codeproject.com/Articles/24065/Paging-ListView-With-DataPager
在本文中,作者使用DataPager_PreRender
将其数据绑定到ListView:
protected void DataPagerProducts_PreRender(object sender, EventArgs e)
{
ProductList db = new ProductList();
this.ListViewProducts.DataSource = db.GellAll();
this.ListViewProducts.DataBind();
}
你可以尝试类似的东西:
protected void MyDataPager_PreRender(object sender, EventArgs e)
{
LoadListview();
}
然后您需要做的就是修改LoadListview()
方法以从DataPager控件本身获取起始索引。
private void LoadListview()
{
string FindWhat = Request.QueryString["Find"];
string FindWhere = Request.QueryString["Where"];
string TownName = FindWhere;
string CountyName = FindWhere;
string PostcodeName = FindWhere;
startIndex = MyDataPager.StartRowIndex * MyDataPager.PageSize;
adap = new SqlDataAdapter("<Query removed to save space>", conn);
DataSet ds = new DataSet();
adap.Fill(ds);
lstBusiness.DataSource = ds;
lstBusiness.DataBind();
}
正如您所看到的,我通过获取行索引并将其乘以DataPager的页面大小来获取startIndex
。现在,您只需将SQL查询的这一部分从startIndex * 80
更改为startIndex
。
如果一切按计划进行,您将不需要btnPrevious
和btnNext
点击事件。
我希望这能让您对自己想要实现的目标有所了解。