Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 27th, 2004, 04:17 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default No viewstate datagrid failure on "more pages"

I have a datagrid with viewstate dissabled (way too many records). On it is a pager which draws out like this:

1 [u]2</u> [u]3</u> [u]4</u> [u]5</u> [u]6</u> [u]7</u> [u]8</u> [u]9</u> [u]10</u> [u]...</u>

Pages 1-10 work fine. Then I click on ... and I get to page 11 and this view:

[u]...</u> 11 [u]12</u> [u]13</u> [u]14</u> [u]15</u> [u]16</u> [u]17</u> [u]18</u> [u]19</u> [u]20</u> [u]...</u>

Then I click on 12 and I end up back on page 3.

[u]1</u> [u]2</u> 3 [u]4</u> [u]5</u> [u]6</u> [u]7</u> [u]8</u> [u]9</u> [u]10</u> [u]...</u>

I have figured out that it has something to do with the lack of viewstate. The current page is now being restored. However, it seems that my assumption that the grid knew what page is associated with the paging link buttons was wrong. It seems that perhaps the target pages are based on the current page, which is always starting at 0. This makes sense because when I clicked 12, it was the 3rd button, so I therefore end up on the 3rd page from the beginning.

Any ideas?
Old May 28th, 2004, 07:19 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts

You can try this but know that this approach works only with database tables that have a column that uniquely indexes each database row.

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>

<Script Runat="Server">

Dim conNorthwind As SqlConnection
Dim strSelect As String
Dim intStartIndex As Integer
Dim intEndIndex As Integer

Sub Page_Load
  Dim cmdSelect As SqlCommand

  conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=North wind" )
  If Not IsPostBack Then
    ' Get Total Pages
    strSelect = "Select Count(*) From Products"
    cmdSelect = New SqlCommand( strSelect, conNorthwind )
    dgrdProducts.VirtualItemCount = ( cmdSelect.ExecuteScalar() / dgrdProducts.PageSize )
  End If
End Sub

Sub BindDataGrid
  Dim dadProducts As SqlDataAdapter
  Dim dstProducts As DataSet

  intEndIndex = intStartIndex + dgrdProducts.PageSize
  strSelect = "Select * From Products Where ProductID > @startIndex " _
    & "And ProductID <= @endIndex Order By ProductID"
  dadProducts = New SqlDataAdapter( strSelect, conNorthwind )
  dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex )
  dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex )
  dstProducts = New DataSet
  dadProducts.Fill( dstProducts )

  dgrdProducts.DataSource = dstProducts
End Sub

Sub dgrdProducts_PageIndexChanged( s As Object, e As DataGridPageChangedEventArgs )
  intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize )
  dgrdProducts.CurrentPageIndex = e.NewPageIndex
End Sub


<form Runat="Server">

  Runat="Server" />


Old May 28th, 2004, 07:26 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

This does little to solve my problem because:

A) It uses custom page, which is probably what should be used instead. (I'm coming on as cleanup crew to this application.)

B) It makes a lot of assumptions that
  1. You are always ordering by ProductID
  2. Your ProductID values start at 1
  3. There are no gaps in your ProductIDs
Old May 28th, 2004, 07:33 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

You could mix standard paging and custom paging a little. Here's what I would do / try

1. Keep track of the current page myself. Store it in view state.

2. In ItemCreated, hook into the footer with the pager bar, and set it to your likings (e.g. hide the first XX pages, shows some others, etc, based on the value of CurrentPage

3. On PageIndexChanged, store the current page in ViewState, and set your grid to the required page.

It may not be elegant, but I think it will work.


Imar Spaanjaars
Everyone is unique, except for me.
Old May 28th, 2004, 01:54 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

Solved the problem:

I hadn't noticed that the datagrid was getting reconstructed (DataBind()) in a call being made in page_init. This was causing all the paging link buttons to get out of whack and behave silly. I moved the call to page_load and it took care of the majority of the problem.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Master Pages, Content Pages and CSS carliviris Visual Studio 2005 0 January 8th, 2008 05:56 PM
Sortable DataGrid within Master Pages template jtoth ASP.NET 1.x and 2.0 Application Design 0 April 13th, 2005 01:46 PM
printing datagrid on asp.net pages sajid C# 0 March 8th, 2005 11:44 AM
Web pages constructing: I-mode(mobile) pages karib Dreamweaver (all versions) 3 June 6th, 2004 09:48 AM
Persisting viewstate between pages drettberg ASP.NET 1.0 and 1.1 Professional 3 September 22nd, 2003 11:30 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.