If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Problem with Gridview and Paging

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-16-09, 17:47
chris3vic chris3vic is offline
Registered User
 
Join Date: Feb 2009
Posts: 11
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>
Reply With Quote
  #2 (permalink)  
Old 03-17-09, 04:40
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
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
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 03-17-09, 05:03
chris3vic chris3vic is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 03-17-09, 05:16
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
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
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 03-17-09, 06:07
chris3vic chris3vic is offline
Registered User
 
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!!!
Reply With Quote
  #6 (permalink)  
Old 03-17-09, 10:00
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Want to show us the clear button?
I'm about to get out of my depth here
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 03-17-09, 12:17
chris3vic chris3vic is offline
Registered User
 
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!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On