Solved

Asp.net 4.00 how to page through Listview data that is created from code behind?

Posted on 2014-01-06
3
478 Views
Last Modified: 2014-01-07
Hi,
I have created a web form that has a number of search options that you can select such as by employee name or customer name etc. Once you have picked what you want to search by there is a text box that you enter your criteria into and a "Search" button that runs a code behind sub that uses the option you chose and the text you entered to create a query that gets the matching data. Once the data is produced the sub then binds it to the ListView control and updates the screen showing the results in the ListView.  

I have added a DataPager so that the multiple page results can be paged through as I don't have enough room to show the full set of results on the web form.  The problem is that because the results are dynamically generated when the user hits the Search button after the first page has been drawn the DataPager appears to be working but the results displayed don't change.  If I go to the last page the results are less than a full page full (I am showing 12 rows per page) the ListView shrinks to the number of actual rows there are on the last page but the data showing is still the items from the first page.

Can anyone tell me what I am doing wrong??

The code that binds the data to the ListView (lstIncidentReports) is like this:

    Private Sub PopulateList(ItemToSelect As Integer, Optional SearchText As String = "")
        Dim i As New IncidentReport
        Dim ins As New IncidentReports
        Dim n As Integer = 0
        Dim SelItem As Integer = 0
        Dim StrSQL As String = ""
        Dim Tempdate As Date = ND


        Try
            ScreenUpdating = True
            lstIncidentReports.Items.Clear()

            If SearchText = "" Then
                ins = i.GetRecordsForSearchList(False)
            Else
                If optCustomerName.Checked Then
                    StrSQL = "Select CustomerName, StoreName, OfficerName, TotalAmount, IncidentReportID  from IncidentReport WHERE CustomerName like '%" & SearchText & "%' AND RecordIsDeletedYN=0 ORDER BY IncidentDate desc;"
                End If
                If optDate.Checked Then
                    If IsDate(SearchText) Then
                        Tempdate = CDate(SearchText)
                        SearchText = Format(Tempdate, "yyyy-MM-dd")
                    End If
                    StrSQL = "Select  CustomerName, StoreName, OfficerName, TotalAmount, IncidentReportID from IncidentReport WHERE IncidentDate = '" & SearchText & "' AND RecordIsDeletedYN=0 ORDER BY IncidentDate desc;"
                End If
                If optIncidentType.Checked Then
                    StrSQL = "Select  CustomerName, StoreName, OfficerName, TotalAmount, IncidentReportID from IncidentReport WHERE TypeOfIncident = '" & SearchText & "' AND RecordIsDeletedYN=0 ORDER BY IncidentDate desc;"
                End If
                If optOfficerName.Checked Then
                    StrSQL = "Select  CustomerName, StoreName, OfficerName, TotalAmount, IncidentReportID from IncidentReport WHERE OfficerName Like '%" & SearchText & "%' AND RecordIsDeletedYN=0 ORDER BY IncidentDate desc;"
                End If
                If optSite.Checked Then
                    StrSQL = "Select  CustomerName, StoreName, OfficerName, TotalAmount, IncidentReportID from IncidentReport WHERE StoreName Like '%" & SearchText & "%' AND RecordIsDeletedYN=0 ORDER BY IncidentDate desc;"
                End If
                ins = i.GetRecordsForSearchList(False, StrSQL)
            End If


            lstIncidentReports.DataSource = Nothing

            If IsNothing(ins) Then
                'No records found.
                Exit Sub
            Else
                lstIncidentReports.DataSource = ins
                lstIncidentReports.DataBind()
            End If

        Catch ex As Exception

            PEH("PopulateList", "frmOfficerIncidentReport", ex.Message)

        Finally

            If Not IsNothing(i) Then
                i.Dispose()
            End If

            ScreenUpdating = False

        End Try
    End Sub

The call to "ins = i.GetRecordsForSearchList(False, StrSQL)" is just a class module that maps to the database table and returns a collection of i records that match the database records, this collection "ins" is then used as the DataSource.

Siv
0
Comment
Question by:Siv
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39761781
It appears that you are setting the ListView.DataSource manually, instead of letting the DataPager  handle the paging itself.

Here is a reference that might help:

Paging ListView With DataPager
http://www.codeproject.com/Articles/24065/Paging-ListView-With-DataPager

<asp:DataPager ID="DataPagerProducts" runat="server" PagedControlID="ListViewProducts"
    PageSize="3" OnPreRender="DataPagerProducts_PreRender">
0
 

Author Comment

by:Siv
ID: 39761817
TheLearnedOne,
Brilliant, the key bit of information I was missing was that you could have the "onPreRender" parameter of the DataPager call a code behind subroutine.  So what I have done is made it call the click routine of the Search button which effectively re-queries the database using the selected Search Option and Search Text and it now works!!

What I don't get is how the DataPager knows where it is in the sequence of returned data as I didn't think this would work as calling the Search click routine I thought would get all the same data again and put you back on the first record, how does it know that I want the second or third page of results?

Siv
0
 

Author Closing Comment

by:Siv
ID: 39761819
Brilliant this article helped me fix the issue.
Thanks for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now