Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 27th, 2004, 04:17 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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?
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie
  #2 (permalink)  
Old May 28th, 2004, 07:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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 )
    conNorthwind.Open()
    dgrdProducts.VirtualItemCount = ( cmdSelect.ExecuteScalar() / dgrdProducts.PageSize )
    conNorthwind.Close()
    BindDataGrid
  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
  dgrdProducts.DataBind()
End Sub

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

</Script>

<html>
<head><title>DataGridCustomPaging.aspx</title></head>
<body>
<form Runat="Server">

<asp:DataGrid
  ID="dgrdProducts"
  AllowPaging="True"
  AllowCustomPaging="True"
  PageSize="3"
  OnPageIndexChanged="dgrdProducts_PageIndexChanged"
  PagerStyle-Mode="NumericPages"
  CellPadding="3"
  Runat="Server" />

</form>
</body>
</html>



  #3 (permalink)  
Old May 28th, 2004, 07:26 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
  #4 (permalink)  
Old May 28th, 2004, 07:33 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #5 (permalink)  
Old May 28th, 2004, 01:54 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:54 PM.


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