繁体   English   中英

使用datapager的Listview自定义分页

[英]Listview custom paging with datapager

谁能告诉我如何使用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();
    }

本文可以为您提供帮助: 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

如果一切按计划进行,您将不需要btnPreviousbtnNext单击事件。

我希望这可以使您对要实现的目标有一些了解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM