2

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

1 回答 1

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();
    }

如您所见,我startIndex通过获取行索引并将其乘以 DataPager 的页面大小来获得。现在您可以将这部分 SQL 查询从startIndex * 80更改为startIndex.

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

我希望这能让您对您想要实现的目标有所了解。

于 2012-04-13T13:25:52.047 回答