Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-01-06
3
Medium Priority
?
500 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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 a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

572 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