Solved

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

Posted on 2014-01-06
3
480 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

26 Experts available now in Live!

Get 1:1 Help Now