TallGuy
TallGuy

Reputation: 1953

How do I connect an ObjectDataSource, a ListView and a DataPager?

When I try to connect together an ObjectDataSource, a ListView and a DataPager, the correct values are not passed to the select method of the ObjectDataSource. -1 is always passed for maximumRows and 0 is passed for startRowIndex.

<asp:DataPager runat="server" ID="Address_DataPager" PageSize="50" PagedControlID="Address_ListView" >
    <Fields>
        <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
            FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
            NextPageText=" &gt; " PreviousPageText=" &lt; " />
    </Fields>
</asp:DataPager>
<asp:ListView ID="Address_ListView" runat="server"
    DataSourceID="Address_DataSource" DataKeyNames="AddressId" >
    <LayoutTemplate>
        <table class="resultsGrid">
            <tr class="gridRow">
                <th scope="col">Address</th>
            </tr>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr class="gridRow">
            <td>
                <asp:Label ID="Address_Label" runat="server" Text='<%# Eval("Address") %>' />
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>
<asp:ObjectDataSource ID="Address_DataSource" runat="server" TypeName="SuperApp.Business.Address"
    SelectMethod="SelectAddress" EnablePaging="True"
    MaximumRowsParameterName="maximumRows"
    StartRowIndexParameterName="startRowIndex" >
    <SelectParameters>
        <asp:Parameter Name="maximumRows"   DefaultValue="10" Type="Int32" />
        <asp:Parameter Name="startRowIndex" DefaultValue="5"  Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

This is the select method in the Address class in the SuperApp.Business namespace:

System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
public DataTable SelectAddress(int maximumRows, int startRowIndex)
{
    if (maximumRows < 1) throw new ArgumentOutOfRangeException("maximumRows", "Maximum rows should be greater than zero.");
    if (startRowIndex < 0) throw new ArgumentOutOfRangeException("startRowIndex", "Start row index should be greater or equal to zero.");
    if (startRowIndex >= maximumRows) throw new ArgumentOutOfRangeException("startRowIndex", "Start row index should be less than the maximum rows.");

    // Would do something interesting here.
    return null;
}

The first exception is always thrown, as maximumRows is always -1. I have tried:

It is probably something obvious that I just can't see. Any suggestions?

Upvotes: 1

Views: 2209

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

Yes, this should be obvious.

You lack the SelectCountMethod on your ObjectDataSource so that not only the current page can be selected but also it is possible to determine how many objects are there.

A working example:

http://netpl.blogspot.com/2009/04/how-to-controll-aspnet-listview-page.html

Upvotes: 2

Related Questions