0

我在 a.aspx 中有带有多页记录的 gridview。当我单击 b.aspx 中的按钮时,我需要转到包含 gridview 中记录的特定页面。

4

1 回答 1

4

我假设您实际上想要转到特定记录。然后我将通过 url 参数(或会话)传递它的 ID,然后使用此记录转到网格中的页面。这更好,因为页面可能会改变(fe 排序)。

假设您的网格列出了产品,并且您希望确保显示特定产品,因为您刚刚在另一个页面上编辑了产品详细信息。还假设你DataSource是一个DataTable(但这并不重要):

/// <summary>
/// Binds the products-GridView.
/// </summary>
/// <param name="ProductID">the ProductID to be displayed, changes also the PageIndex if necessary</param>
private void BindProductGrid(int ProductID = -1)
{
    DataTable tblProducts = getAllProducts();
    GridProducts.DataSource = tblProducts;
    bool needsPaging = (tblProducts.Rows.Count / GridProducts.PageSize) > 1;

    if (ProductID == -1)
    {
        this.GridProducts.PageIndex = 0;
        this.GridProducts.SelectedIndex = -1;
    }
    else
    {
        int selectedIndex = tblProducts.AsEnumerable()
            .Select((Row, Index) => new { Row, Index })
            .Single(x => x.Row.Field<int>("ProductID") == ProductID).Index;
        int pageIndexofSelectedRow = (int)(Math.Floor(1.0 * selectedIndex / GridProducts.PageSize));
        GridProducts.PageIndex = pageIndexofSelectedRow;
        GridProducts.SelectedIndex = (int)(GridProducts.PageIndex == pageIndexofSelectedRow ? selectedIndex % GridProducts.PageSize : -1);
    }
    GridProducts.DataBind();
}
于 2012-11-09T11:17:26.160 回答