Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    11

    Unanswered: Problem with Gridview and Paging

    I have a gridview on my application that is populated from a recordset after a user selects criteria from a host of drop down lists etc. This is working fine.

    However, due to the possible and probable large number of records that will be returned I decided to use paging to make it visually easier to digest.

    The problem I'm having is that after the grid loads the records and you attempt to click to another page od records, the whole gridview dissapears. I am obviously missing something important but I just cannot figure out what.

    Here is the code for loading the grid: (by the way, I am testing this on a small access database just to get the fundamentals right)


    Code:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\Chris\Desktop\incidents.mdb"
            con.Open()
            sql = "SELECT * FROM tbl_incidents WHERE Address Like '%" & txtAddress.Text & "%' And Borough Like '%" & ddlborough.SelectedValue & "%'"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "incidents")
            GridView1.DataSource = ds.Tables("incidents")
            GridView1.DataBind()
    
            If GridView1.Rows.Count > 0 Then
                lblRowCount.Text = GridView1.Rows.Count
            Else : lblRowCount.Text = "No"
            End If
            con.Close()
        End Sub
    And here is the event handler for the page index changing


    Code:
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
            GridView1.PageIndex = e.NewPageIndex
            GridView1.DataBind()
        End Sub
    Here is the grid view properties in ASP


    Code:
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
                GridLines="None" Height="135px" Width="1218px" AllowPaging="True" 
                PageSize="2" OnPageIndexChanging="GridView1_PageIndexChanging">
                <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Is it because you're binding a second time..?

    I think you need to utilise the Page.IsPostBack method to determine whether or not you need to rebind the control.

    ...maybe...
    George
    Home | Blog

  3. #3
    Join Date
    Feb 2009
    Posts
    11
    Yeah, I assume it is something to do with potback as the grid is filled dynamically using a command button that issues a sql query to the database and fills a dataset. The grid is then boun to that dataset.

    Upon postback, it appears that the data is 'lost'

    Not sure on the postback method, I'm newish to asp.net. Can you advise on that?

    How do I honestly avoid binding twice though? I need to bind on triggering the sql query to get the initial load through the cmd button , and then re-bind again on pageindexchanging.

    Confusing me this one because the grid is created dynamically and all the help out there is for using datasources that populate a grid on page load, and that wont work for me.

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    My knowledge of .NET is very limited, but here's what I think is the answer:
    Code:
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
            GridView1.PageIndex = e.NewPageIndex
            If not Page.IsPostBack Then
                GridView1.DataBind()
            End If
        End Sub
    George
    Home | Blog

  5. #5
    Join Date
    Feb 2009
    Posts
    11
    Ok, I just tried that, thank you for the advice, I'm on the right path here.

    Code:
        Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
            GridView1.PageIndex = e.NewPageIndex
            If Not Page.IsPostBack Then
                GridView1.DataSource = ds.Tables("incidents")
                GridView1.DataBind()
            End If
        End Sub
    Now the weird thing is, when i click page 2 (there are only 2 pages) the datagrid still disappears, however, if I click the clear button (this button clears all search boxes ready for the next search) the grid re-appears on the 2nd page, but with the first page results!!!

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Want to show us the clear button?
    I'm about to get out of my depth here
    George
    Home | Blog

  7. #7
    Join Date
    Feb 2009
    Posts
    11
    sure

    theclear button has an onlick event handler which just says

    txtbox1.text = ""
    txtbox2.text = ""
    dropdown1.clearselection()
    dropdown2.clearselection()
    and so on

    even with the onclick event removed so the utton does absolutely nothing, it still makes the gridview re-appear in the same way. I guess it triggers a postback or something.

    There is something fundamental that is missing here I'm sure but I cannot put my finger on it!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •