views:

113

answers:

3

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!

A: 

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.

Obalix
Yeah, I've tried this one, still 0.
David Archer
In which event is your binding code located?
Obalix
A: 

Hmm... never mind on this one. I deleted the GridView and added another one, added the event for PageIndexChanging, then used e.NewPageIndex. For some reason, it wouldn't allow me to use that event on the other GridView. Weird.

David Archer
A: 

Check your post back, you're probably loading the grid fresh on every page load, so when you page, you're probably calling the code that populates your grid and it resets the page index. You need to make sure to load it only if it is not a postback, if it is a post back you'll have to fetch the data and the appropriate pageIndex from a storage area like ViewState etc.

RandomNoob