Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 18th, 2011, 05:24 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default The GridView 'GridView' fired event PageIndexChanging which wasn't handled

Hi

I'm using an ADO.NET Entity Data Model to populate a GridView using the following code

If CriteriaDropDownList.SelectedValue = "Publication.Title" Then
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim allPublications = From publication In myLiteratureCatalogue.Publications
From subject In myLiteratureCatalogue.Subjects
Where publication.SubjectID = subject.SubjectID
Where publication.Title.Contains(CriteriaTextBox.Text)
Order By publication.Title Ascending
Select publication.PublicationID, publication.Title, subject.SubjectType

GridView.DataSource = allPublications
GridView.DataBind()
End Using

The above Linq query is executed by a button click event and executes a search facility of publication titles listed within my sql database.

The results are returned as expected into the browser but with a significant flaw, when the second page is selected from the paging option at the bottom of the gridview I get redirected to an error page stating "The GridView 'GridView' fired event PageIndexChanging which wasn't handled".

Everywhere I have looked states this error is down to the sorting of the gridview results, but I thought I have overcome this problem by sorting the results with "Order By publication.Title Ascending".

Has anyone else experienced similar problems?
Reply With Quote
  #2 (permalink)  
Old February 18th, 2011, 05:37 PM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Take a look here at this thread as I think it deals with the exact same issue: Grid View Problem

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (February 19th, 2011)
  #3 (permalink)  
Old February 18th, 2011, 05:47 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default The GridView 'GridView' fired event PageIndexChanging which wasn't handled

Hi Imar

Thank you for the quick response but I don't really understand the previous post you linked me to. I assume it is the part in bold below which is missing from my code

Me.GridView1.PageIndex = e.NewPageIndex
Me.GridView1.DataSource = GetDataSource()
Me.GridView1.DataBind()

If CriteriaDropDownList.SelectedValue = "Publication.Title" Then
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim allPublications = From publication In myLiteratureCatalogue.Publications
From subject In myLiteratureCatalogue.Subjects
Where publication.SubjectID = subject.SubjectID
Where publication.Title.Contains(CriteriaTextBox.Text)
Order By publication.Title Ascending
Select publication.PublicationID, publication.Title, subject.SubjectType

GridView.DataSource = allPublications
GridView.DataBind()

End Using
Reply With Quote
  #4 (permalink)  
Old February 19th, 2011, 04:27 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Where exactly do you call the code you posted in your first message? In Page_Load alone? If so, you need to call it in PageIndexChanging as well. E,.g you can move your code to a method like GetDataSource and then call GetDataSource () in both Page_Load and in PageIndexChanging. In the latter method you end up with

Code:
 
Me.GridView.PageIndex = e.NewPageIndex
Me.GridView.DataSource = GetDataSource()
Me.GridView.DataBind()
Alternatively, you can repeat the code that accesses your Entities in PageIndexChanging, but that feels like duplicating a lot of code.

For clarity, it's probably better to rename your GridView.GridView is also the type of the control, so giving it the same ID may be asking for trouble (for example, you now need the Me prefix).

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (February 19th, 2011)
  #5 (permalink)  
Old February 19th, 2011, 08:23 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default The GridView 'GridView' fired event PageIndexChanging which wasn't handled Reply to T

Hi Imar

Once again thank you for responding to my enquiry. The page I have designed has three main controls; DropDownList, TextBox and Button. The user will select their search category(table to query in my sql database) from the dropdownlist value. The textbox will hold their search criteria and the button control is used to execute the below code which performs the search facility.

I have two seperate tables I wish to search hence the reason why I have a below IF statement to determine which table to perform the seach on.


Protected Sub CriteriaButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CriteriaButton.Click
LabelNull.Visible = False

If CriteriaDropDownList.SelectedValue = "Publication.Title" Then
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim allPublications = From publication In myLiteratureCatalogue.Publications
From subject In myLiteratureCatalogue.Subjects
Where publication.SubjectID = subject.SubjectID
Where publication.Title.Contains(CriteriaTextBox.Text)
Order By publication.Title Ascending
Select publication.PublicationID, publication.Title, subject.SubjectType

GridView.DataSource = allPublications
GridView.DataBind()
If GridView.Rows.Count = 0 Then
LabelNull.Visible = True
End If
End Using
ElseIf CriteriaDropDownList.SelectedValue = "Subject.SubjectType" Then
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim allSubjects = From publication In myLiteratureCatalogue.Publications
From subject In myLiteratureCatalogue.Subjects
Where publication.SubjectID = subject.SubjectID
Where subject.SubjectType.Contains(CriteriaTextBox.Text)
Order By publication.Title Ascending
Select publication.PublicationID, publication.Title, subject.SubjectType

GridView.DataSource = allSubjects
GridView.DataBind()
If GridView.Rows.Count = 0 Then
LabelNull.Visible = True
End If
End Using

End If

End Sub

End Class


Thanks for all your help.

Phil
Reply With Quote
  #6 (permalink)  
Old February 19th, 2011, 08:30 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

The same principle applies: move the code from CriteriaButton_Click to a separate sub like SetDataSource and call that new method from both CriteriaButton_Click and PageIndexChanging.

When you move to a new page, the existing data is lost. Additionally, CriteriaButton_Click isn't fired, and thus the GridView is empty. By calling the data binding code again in PageIndexChanging, you reassign the data source. The GridView then takes care of displaying the correct page.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (February 19th, 2011)
  #7 (permalink)  
Old February 19th, 2011, 08:59 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default The GridView 'GridView' fired event PageIndexChanging which wasn't handled

Hi Imar

I've moved the code as per your instruction but the new method I have placed it in is causing problems at runtime. What would be an appropriate datatype for the method statement?

Public Sub SetDataSource(ByVal e As System.EventArgs)

If CriteriaDropDownList.SelectedValue = "Publication.Title" Then
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim allPublications = From publication In myLiteratureCatalogue.Publications
From subject In myLiteratureCatalogue.Subjects
Where publication.SubjectID = subject.SubjectID
Where publication.Title.Contains(CriteriaTextBox.Text)
Order By publication.Title Ascending
Select publication.PublicationID, publication.Title, subject.SubjectType
End Using
ElseIf CriteriaDropDownList.SelectedValue = "Subject.SubjectType" Then
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim allSubjects = From publication In myLiteratureCatalogue.Publications
From subject In myLiteratureCatalogue.Subjects
Where publication.SubjectID = subject.SubjectID
Where subject.SubjectType.Contains(CriteriaTextBox.Text)
Order By publication.Title Ascending
Select publication.PublicationID, publication.Title, subject.SubjectType
End Using

End If

End Sub
Reply With Quote
  #8 (permalink)  
Old February 19th, 2011, 09:09 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

None; just:

Code:
 
Public Sub SetDataSource()
 ...
End Sub
should work.

When you post code, can you please use the Code Formatting button on the toolbar (#)? Makes your code easier to read.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #9 (permalink)  
Old February 19th, 2011, 09:18 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default

Hi Imar

Won't the variables need to be stored as a datatype? so the SetDataType call in the button click event would know what to bring through? At present that part is indicating an error as the method does not produce a value?


Code:
Public Sub SetDataSource()

Dim allPublications = ???

        If CriteriaDropDownList.SelectedValue = "Publication.Title" Then
            Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
                Dim allPublications = From publication In myLiteratureCatalogue.Publications
                                      From subject In myLiteratureCatalogue.Subjects
                                      Where publication.SubjectID = subject.SubjectID
                                      Where publication.Title.Contains(CriteriaTextBox.Text)
                                      Order By publication.Title Ascending
                                      Select publication.PublicationID, publication.Title, subject.SubjectType
            End Using
        ElseIf CriteriaDropDownList.SelectedValue = "Subject.SubjectType" Then
            Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
                Dim allPublications = From publication In myLiteratureCatalogue.Publications
                                      From subject In myLiteratureCatalogue.Subjects
                                      Where publication.SubjectID = subject.SubjectID
                                      Where subject.SubjectType.Contains(CriteriaTextBox.Text)
                                      Order By publication.Title Ascending
                                      Select publication.PublicationID, publication.Title, subject.SubjectType
            End Using

        End If

    End Sub


    Protected Sub CriteriaButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CriteriaButton.Click

        LabelNull.Visible = False

        Me.GridView.DataSource = SetDataSource()
        Me.GridView.DataBind()
        If GridView.Rows.Count = 0 Then
            LabelNull.Visible = True
        End If
Reply With Quote
  #10 (permalink)  
Old February 19th, 2011, 09:38 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Well, if you're returning the types from Sub, then yes. In your case, you're using projection to create anonymous types (e.g. you're selecting "random" properties from your publication and subject types) which makes this pretty difficult. To make this a lot easier, simply bind the GridView in your method as well. That's also why I renamed the suggested method from GetDataSource to SetDataSource earlier. So, something like this:

Code:
 
Protected Sub CriteriaButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CriteriaButton.Click
 
  ' Do other stuff here if needed
  SetDataSource()
End Sub
 
Protected Sub GridView_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles Me.GridView.PageIndexChanging
 
  Me.GridView.PageIndex = e.NewPageIndex
  SetDataSource()
 
End Sub
 
Private Sub SetDataSource()
  If CriteriaDropDownList.SelectedValue = "Publication.Title" Then
    Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
                Dim allPublications = From publication In myLiteratureCatalogue.Publications
                      From subject In myLiteratureCatalogue.Subjects
                      Where publication.SubjectID = subject.SubjectID
                      Where publication.Title.Contains(CriteriaTextBox.Text)
                      Order By publication.Title Ascending
                      Select publication.PublicationID, publication.Title, subject.SubjectType
      GridView.DataSource = allPublications
      GridView.DataBind()
    End Using
  ElseIf CriteriaDropDownList.SelectedValue = "Subject.SubjectType" Then
     Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
           Dim allSubjects = From publication In myLiteratureCatalogue.Publications
                      From subject In myLiteratureCatalogue.Subjects
                      Where publication.SubjectID = subject.SubjectID
                      Where subject.SubjectType.Contains(CriteriaTextBox.Text)
                      Order By publication.Title Ascending
                      Select publication.PublicationID, publication.Title, subject.SubjectType
      GridView.DataSource = allSubjects
      GridView.DataBind()
    End Using
  End If
  If GridView.Rows.Count = 0 Then
    LabelNull.Visible = True
  End If
End Sub
Note: untested code and typed directly in this post, but I hope you get the idea.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview page_Indexing event vijay@iBtions ASP.NET 2.0 Basics 3 January 25th, 2011 03:03 AM
Nested Gridview Event Problem surendraparashar ASP.NET 2.0 Professional 0 November 16th, 2007 03:17 AM
GridView SelectedIndexChanged event bnorg ASP.NET 2.0 Professional 1 July 7th, 2007 01:14 AM
Gridview Sorting Event without a Datasource rit01 ASP.NET 2.0 Professional 15 March 22nd, 2007 03:34 AM
fired event Sorting which wasn't handled fantef ASP.NET 2.0 Basics 0 July 27th, 2006 10:05 AM



All times are GMT -4. The time now is 11:18 AM.


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