PageIndexChanging in GridView in ASP.NET

一个人想着一个人 提交于 2019-12-20 11:19:06

问题


I have a gridview which I am using to display a dataset result. The problem is I am using paging in it. But when I click on the page # it says that I haven't handled the event. Do I need to rebind the dataset???

Thanks


回答1:


Try the following code:

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



回答2:


Try it

In the pageload

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

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

In the pageindexchanging

GridView1.PageIndex = e.NewPageIndex;

loadGrid();



回答3:


In VB.net, it does not have much difference with C#, you just remove the semicolons at the end of each line

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



回答4:


You should set the .PageIndex before binding data! Otherwise, you would need extra clicks which actually double the BindData method calls. The following is my tested vb code.

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


来源:https://stackoverflow.com/questions/4665613/pageindexchanging-in-gridview-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!