【问题标题】:How do I connect an ObjectDataSource, a ListView and a DataPager?如何连接 ObjectDataSource、ListView 和 DataPager?
【发布时间】:2012-07-30 03:34:56
【问题描述】:

当我尝试将 ObjectDataSource、ListView 和 DataPager 连接在一起时,没有将正确的值传递给 ObjectDataSource 的 select 方法。 maximumRows 始终传递 -1,startRowIndex 始终传递 0。

<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>

这是SuperApp.Business命名空间中Address类中的select方法:

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;
}

第一个异常总是被抛出,因为maximumRows 总是-1。我试过了:

  • 将 DataPager 移动到 LayoutTemplate 中。
  • 将 DataPager 移入 LayoutTemplate 并移除 PagedControlID 值。
  • 将 DefaultValue 值更改为各种东西。
  • 从参数中删除默认值。
  • 从参数中删除类型。
  • 完全删除 SelectParameters。

这可能是我看不到的显而易见的事情。有什么建议吗?

【问题讨论】:

    标签: asp.net listview objectdatasource


    【解决方案1】:

    是的,这应该很明显。

    您的ObjectDataSource 上缺少SelectCountMethod,这样不仅可以选择当前页面,还可以确定其中有多少对象。

    一个工作示例:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多