Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 08, 2012 04:17 PM by DeepPowder
Nov 07, 2012 02:34 PM|LINK
I would like to return the total number of records in a ListView that has a DataPager control. If I use
Int32 numRecs = ListView1.Items.Count
I get the number of records on the current page, not all the records.
What is the best way to get the total number of records in a ListView?
Thanks for your help
Nov 07, 2012 03:32 PM|LINK
Not sure, but try using the property TotalRowCount from DataPager.
Hope this helps.
Nov 07, 2012 03:41 PM|LINK
Thanks for your reply. I found this too and I'm trying to get it to work. Here's my code
DataPager dtp1 = (DataPager) ListView1.FindControl("DataPager1');
Int32 numRecs = dtp1.TotalRowCount;
THe code fails on the second line. It doesn't like dtp1.
Nov 07, 2012 03:55 PM|LINK
Check this link...
Nov 07, 2012 03:57 PM|LINK
I believe it will be necessary to declare the DataPager out of ListView.
Nov 08, 2012 01:07 AM|LINK
You can get the total using the datasource
Nov 08, 2012 04:17 PM|LINK
Thanks for all your help. I finally found the solution, shown below.
1) DataPager1 is ListView Control and is rendered inside the ListView.
2) Copy the DataPager to create a second DataPager DataPager2 outside of and below the ListView.
Remove MaxSize Property and all DataPager Fields and add Property to DataPager: PagedControlID="ListView1"
3) Use the ListView_DataBound Event shown below.
3) Set the ListView.Datasource = null in the Page_Load Event. This is necessary to prevent the ListView
from displaying Max Size empty rows after the Datasource is cleared and the page is submitted again
by clicking on the [Clear Selection] Button and the ListView should return no records.
protected void ListView1_DataBound(object sender, EventArgs e)
DataPager dtp1 = (DataPager)ListView1.FindControl("DataPager1");
DataPager dtp2 = (DataPager)DataPager2;
Int32 numRecs = (Int32)dtp2.TotalRowCount;
String sMsg = "";
if ((dtp1 == null) ||(numRecs == 0))
sMsg = "Enter Selection Criteria and click [Show Selection] Button. ";
else if (numRecs == 1)
sMsg = "1 record found. ";
sMsg = numRecs + " records found. ";
lblMessage.Text = sMsg;