[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Datagrid Paging - Not paging past second page

I have a datagrid that only pages to the second page, even though there is enough data to fill atleast 20 or so pages.  Below is the datagrid & OnPageIndexChanged sub:

<ASP:DataGrid id="MyDataGrid" runat="server"
    Width="855"
    BackColor="#cccccc"
    BorderColor="#FFFFFF"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana" Height="16px" BorderWidth="2"
    Font-Size="11px"
    HeaderStyle-BackColor="#999999" HeaderStyle-Height="16px" HeaderStyle-Font-Size="11px"
    EnableViewState="false"
    HorizontalAlign="Center"
    AllowPaging="true"
    PageSize="8"  
    AllowCustomPaging="false"
    OnPageIndexChanged="MyDataGrid_PageIndChange"
/>

  Sub MyDataGrid_PageIndChange(s As Object, e As DataGridPageChangedEventArgs)
      MyDataGrid.CurrentPageIndex = e.NewPageIndex
      BindDataGrid
  End Sub

  Sub BindDataGrid
     Dim dr1 As SqlDataAdapter
     Dim ds1 As DataSet
     dr1 = New SqlDataAdapter(query, MyConnection)
     ds1 = New DataSet
     dr1.Fill(ds1)
            
     MyDataGrid.DataSource = ds1
     MyDataGrid.DataBind()
End Sub

BindDataGrid is also called in the page_load event.  If i place this in 'If Not Page.IsPostBack' the second page does not contain any data at all so I have BindDataGrid occuring in every page_load.  Looking for an answer as to why this dg will not page past the second page.  (since page size is 8 I can only see the first 16 records)

Thanks in advance
0
egdigital
Asked:
egdigital
1 Solution
 
ayha1999Commented:
Hi,

change to

Sub BindDataGrid
     Dim dr1 As SqlDataAdapter
     Dim ds1 As DataSet
     dr1 = New SqlDataAdapter(query, MyConnection)
     ds1 = New DataSet
     dr1.Fill(ds1)

Session("YouVariable")=ds1         <-------------------------- add this line
         
     MyDataGrid.DataSource = ds1
     MyDataGrid.DataBind()
End Sub

Sub MyDataGrid_PageIndChange(s As Object, e As DataGridPageChangedEventArgs)
      MyDataGrid.CurrentPageIndex = e.NewPageIndex
      MyDataGrid.DataSource=Session("YourVariable") <--------- add this
      MyDataGrid.DataBind()       <-------- add this
  End Sub

hope this helps.

ayha
0
 
Jens FiedererCommented:
Ayha's solution will allow you NOT to use BindDataGrid in every pageload, only if not page.ispostback
0
 
raterusCommented:
Keep in mind, when you store the data in the session like this, your user isn't going to see any changes someone may be doing to the data until they close their browser.  If this is, was, or ever could be a multi-user app, I wouldn't use this approach.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
egdigitalAuthor Commented:
What is the alternative to using the session approach?  I still am not sure why my original means of paging only went to the second page & not subsequent pages (3, 4, etc)
0
 
raterusCommented:
Yeah, the last posts have nothing to do with your actual problem, my guess is you are rebind the datagrid on a postback, you should have something like this in Page_Load

Sub Page_load(...)
  If Page.IsPostBack Then
    BindDataGrid()
  End If
End Sub
0
 
raterusCommented:
ahh yes, you already mentioned this, then your real problem here is you set enableviewstate=false in the datagrid, you have to set this to true.
0
 
egdigitalAuthor Commented:
Enabling the viewstate & placing the bind in the pageload between 'If Not Page.IsPostBack'  block did the trick.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now