tags:

views:

23

answers:

1

Need that for ASP.NET site, preferable C# but VB will be just fine.

title says all. I guess we all get into situation like this one on daily basis. Pager as UI is useless if there is less than PageSize items in DataObject binded to ListView or other types of Dababindable objects.

So the question how to disable it////

A: 

You could hide it in DataBound Event of your Listview when your Datasource's RowCount is less/equal the Pager's PageSize. For example(pseudo-code, change yourDataSource):

Private Sub ListView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DataBound
        Dim dataPager As DataPager = DirectCast(ListView1.FindControl("DataPager1"), DataPager)
        dataPager.Visible = yourDataSource.Rows.Count > dataPager.PageSize
End Sub

EDIT: If you used an ObjectDataSource you can catch its Selected Event to access the Datasource (C#):

protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
      // Get total count from the ObjectDataSource
      DataTable dt = e.ReturnValue as DataTable;

      int totalRecordCount = dt.Rows.Count;      
 }

VB:

 Private Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs) Handles ObjectDataSource1.Selected
        ' Get total count from the ObjectDataSource
        Dim dt As DataTable = DirectCast(e.ReturnValue, DataTable)
        Dim totalRecordCount As Int32 = dt.Rows.Count
 End Sub

Then you can use my code above to switch visibility of the Pager.

Tim Schmelter
@Tim Schmelter, thanks for quick reply but i have one problem my DataSource is ObjectDataSource which doesn't have Rows. What can i do to avoid it?
eugeneK
I have edited my answer. Does this work for you(not tested)?
Tim Schmelter
It supposed to work but i wouldn't check that simply because i use too much resources getting the info. you create Datatable which will be create on each page access. too heavy but should work. thanks for pointing me to proper ListView and DataSource events.
eugeneK
That code doesnt create a Datatable. It only gets the reference to the already existing datatable of the ObjectDataSource. http://stackoverflow.com/questions/389571/can-you-get-a-datatable-from-an-objectdatasource
Tim Schmelter