views:

664

answers:

3

Using a standard ASP.NET ListView with a LinqDataSource and pagination enabled (with a DataPager), what would be the best way to default to displaying the last page of results?

+1  A: 

set the current page index to be the page count - 1.

Kon
Yes, but where can the page count be found, and where can you set the page index?
Ryan Versaw
+1  A: 

You'll need to know the total record count and the number of records displayed on a page.

This helpful post shows you how to get the record count:

private LinqDataSourceSelectEventArgs args;
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
     args = e;           
     e.Result = new Database().Table.Whatever...                      
}

protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
     this.label1.Text = args.Arguments.TotalRowCount + " records";
}

There's further discussion there of a situation that sounds similar to yours.

DOK
This will provide us with a row count, and from there we can determine the number of pages present, but I still see no way of setting the page index.
Ryan Versaw
+1  A: 

I've never done this, but there are several places I would look to see if I could make the change you want: an OnSelecting handler for the data source, OnPreRender or OnDataBinding for the ListView, and OnPreRender for the DataPager. Essentially, you want to handle the case of !IsPostBack in a special way. I would probably look at the DataPager first and see if you can find out how many pages there are and set it to the last page. You may need to rebind the data source after setting the page to the one you want. After that I'd look at adding an OnDataBinding handler for the ListView and see what you can do there. I suspect that PreRender happens too late for the ListView to have any effect and OnSelecting, while good for filtering via a table-based function, probably won't do much good in this case.

tvanfosson