Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 August 17th, 2004, 07:21 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default Paging with DataGrid attach to DDL Question

Hello
I have a DDL Category which list 3 categories.
When nothing is selected or ALL is selected, then it shows all the items in DataGrid.
If one category is selected, it autopostbacks and only these item of that category is shown in DataGrid.

This all works fine.
DataGrid has paging which works fine too.

Everything works good except one thing.

The "show all" page or first displayed page has 3 pages. When I am on the first page everything works but when I am on the 2nd page and I try to select category from DDL I get error:

Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

What does this mean?

Code:

Public Class Book
    Inherits System.Web.UI.Page
    Dim iSelect As String
    Dim dsBookCategory As DataSet
    Dim dsBook As DataSet
    Dim dr As DataRow

     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dsBookCategory = DB.GetBookDDLCategory
        dsBook = DB.GetDGBook
        If Not IsPostBack Then
            Me.LoadddlCategory()
            ddlBook.SelectedIndex = -1
            dgBook.DataSource = dsBook
            dgBook.DataBind()
        Else
            Me.DGLoad()
        End If
    End Sub

    Private Sub LoadddlCategory()
        ddlBook.Items.Add("All Books")
        For Each dr In dsBookCategory.Tables("BookDDLCategory").Rows
            ddlBook.Items.Add(dr("CategoryName"))
        Next
        ddlBook.DataBind()
    End Sub

    Private Sub DGLoad()
        If ddlBook.SelectedIndex = 0 Then
            dgBook.DataSource = dsBook
            dgBook.DataBind()
        Else
            iSelect = ddlBook.SelectedValue
            Dim dvBook As New DataView(dsBook.Tables("DGBook"))
            dvBook.RowFilter = "CategoryName ='" & iSelect & "'"
            dgBook.DataSource = dvBook
            dgBook.DataBind()
        End If
    End Sub

    Private Sub ddlBook_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgBook.SelectedIndexChanged
        iSelect = ddlBook.SelectedItem.Value
    End Sub


    Private Sub dgBook_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles dgBook.PageIndexChanged
            dgBook.CurrentPageIndex = e.NewPageIndex
            dgBook.DataBind()
    End Sub

    Private Sub dgBook_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgBook.SelectedIndexChanged

    End Sub
End Class

PLEASE HELP ME

 
Old August 17th, 2004, 10:20 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The error explains it all. The grid is trying to display a page that doesn't exist. When you change categories, you are changing to a category that returns fewer rows than the one you are on. Because the datagrid maintains the pagenumber that you were on (2), and you now have only 1 page of data, it breaks. The easiest solution would be to reset the datagrid page index to 0 when you change categories.
 
Old August 17th, 2004, 11:12 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello
Thanks that makes sense. I looked it up in MSDN and it told me to use
Try Catch Finally but I can't get this to work.

MSDN said to make:
page event handler simply set the currentpageindex = 0

 Private Sub dgBook_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles dgBook.PageIndexChanged
 Try
            dgBook.CurrentPageIndex = e.NewPageIndex

        Catch ePage As ArgumentOutOfRangeException

        Finally
            dgBook.CurrentPageIndex = 0
        End Try

But when I do this I can't get to page 2.

Can you post some code for me.

 
Old August 19th, 2004, 09:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You are trying to do this check where you change the page index. You need to do this check where you bind the datagrid. In your PageIndexChanged handler, just set the page index and call the method that binds the grid. When you bind the grid you need to do the check. Unfortunately, all you get for an exception is a general exception (instead of an "ArgumentOutOfRangeException") so you must check for the details of the exception.

This is what works for me inside my "BindMyGrid()" method:

'Need to try binding once, and check for a page index fault
Try
    grdSR.DataBind()
Catch ex As Exception
    'Bind again at page 0
    If ex.Message.IndexOf("CurrentPageIndex") > 0 Then
        grdSR.CurrentPageIndex = 0
        grdSR.DataBind()
    End If
End Try
 
Old August 22nd, 2004, 11:05 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you planoie!
That really worked!






Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedIndexChanged DDL-->in Datagrid slaverymorgue ASP.NET 2.0 Basics 1 February 19th, 2007 09:30 AM
DropDownList into DataGrid-Every Row has a DDL su C# 2005 2 November 30th, 2006 10:23 AM
DropDownList into DataGrid-Every Row has a DDL su ASP.NET 1.0 and 1.1 Basics 0 November 29th, 2006 05:39 PM





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