Solved

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

Posted on 2014-01-06
3
489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

635 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