Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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