ASP.NET中GridView中PageIndex的更改

cbjzeqam  于 2023-03-31  发布在  .NET
关注(0)|答案(5)|浏览(184)

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

kgsdhlau

kgsdhlau1#

请尝试以下代码:

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

eivgtgni2#

试试看
在页面加载量中

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

camsedfj3#

在www.example.com中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
vbkedwbf

vbkedwbf4#

你应该在绑定数据之前设置.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
e0uiprwp

e0uiprwp5#

除了其他答案,请记住,更改页面也会触发OnRowCommand事件,其中CommandName为“Page”。请注意不要在该方法中放置冲突代码,这可能会改变OnPageIndexChanging操作的结果。

相关问题