ASP.NET中的GridView中的PageIndexChanging

问题描述 投票:23回答:4

我有一个gridview,我用它来显示数据集结果。问题是我在其中使用分页。但是,当我点击页面#时,它表示我没有处理该事件。我需要重新绑定数据集???

谢谢

c# asp.net .net gridview page-index-changed
4个回答
31
投票

请尝试以下代码:

protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    FillGrid();
    grdView.PageIndex = e.NewPageIndex;
    grdView.DataBind();
}

4
投票

试试吧

在页面加载中

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        loadGrid();
    }
}

在pageindexchanging中

private void loadGrid()
{
    using (your_bankEntities context = new your_bankEntities()) //use your connection .edmx
    {
        var jmDados = (from jm in context.yourdbo  orderby jm.your fieldkey  
                         select new
                           {
                               jm.Field1,
                               jm.Field2,
                               jm.Field3,
                               jm.Field4,
                               ........ 
                               jm.n

                           }).ToList();
        GridView1.DataSource = jmDados;

        GridView1.DataBind();
    }
}

在pageindexchanging中

GridView1.PageIndex = e.NewPageIndex;

loadGrid();

1
投票

在VB.net中,它与C#没有太大区别,你只需删除每行末尾的分号

Private Sub myGridview_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles myGridview.PageIndexChanging

   LoadGridView() //Call your method to load the data into the grid.
   myGridview.PageIndex = e.NewPageIndex
   myGridview.DataBind()

End Sub

0
投票

你应该在绑定数据之前设置.PageIndex!否则,您需要额外的点击,这实际上是BindData方法调用的两倍。以下是我测试过的vb代码。

Private Sub GridViewL_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridViewL.PageIndexChanging

    GridViewL.PageIndex = e.NewPageIndex
    BindData()  ' your method to bind data to the grid
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.