ASP.Net Gridview paging, pageindex always == 0

David Archer picture David Archer · May 26, 2010 · Viewed 8.5k times · Source

EDIT: Now Working, see below.

Hi all,

Having a slight problem with my ASP.Net 3.5 app. I'm trying to get the program to pick up what page number has been clicked. I'm using ASP.Net's built in AllowPaging="True" function. It's never the same without code, so here it is:

ASP.Net:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="Vertical" Width="960px" AllowSorting="True" 
            EnableSortingAndPagingCallbacks="True" AllowPaging="True" PageSize="25" >
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

C#:

var fillTable = from ft in db.IncidentDatas
                                where ft.pUserID == Convert.ToInt32(ClientList.SelectedValue.ToString())
                                select new
                                {
                                    Reference = ft.pRef.ToString(),
                                    Date = ft.pIncidentDateTime.Value.Date.ToShortDateString(),
                                    Time = ft.pIncidentDateTime.Value.TimeOfDay,
                                    Premesis = ft.pPremises.ToString(),
                                    Latitude = ft.pLat.ToString(),
                                    Longitude = ft.pLong.ToString()
                                };
                if (fillTable.Count() > 0)
                {
                    GridView1.DataSource = fillTable;
                    GridView1.DataBind();
                    var IncidentDetails = fillTable.ToList();
                    for (int i = 0; i < IncidentDetails.Count(); i++)
                    {
                        int pageno = GridView1.PageIndex;
                        int pagenostart = pageno * 25;
                        if (i >= pagenostart && i < (pagenostart + 25))
                        {
                            //Processing
                        }
                    }
                 }

Any idea why GridView1.PageIndex is always = 0? The thing is, the processing works correctly for the grid view.... it will always go to the correct paging page, but it's always 0 when I try to get the number. Help!

Answer

AxelEckenberger picture AxelEckenberger · May 26, 2010

Have you tried accessing GridView1.PageIndex before calling GridView1.DataBind? It might get reset when you assigning a the new data source and then bind it to the grid.