指数超出范围。在listview中使用datapager时,必须是非负数且小于集合的大小

时间:2016-07-01 06:48:02

标签: asp.net vb.net listview

在我的listview中有datapager。当我点击分页项时,它会抛出异常

  

System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小

VB代码

Protected Sub OnPagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
    TryCast(products.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
    Dim str As String
    Try
        str = "select * from products where status = 'active'"
        Dim conString As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
        Dim con As New MySqlConnection(conString)
        Dim cmd As New MySqlCommand(str)
        con.Open()
        Dim da As New MySqlDataAdapter()
        cmd.Connection = con
        da.SelectCommand = cmd
        Dim dt As New DataTable()
        da.Fill(dt)
        products.DataSource = dt
        products.DataBind()
        itemCount.Text = dt.Rows.Count.ToString
        con.Close()
    Catch ex As Exception
        Response.Write(ex)
    End Try
End Sub

ListView Aspx

<asp:ListView ID="products" runat="server" DataKeyNames="ID" OnPagePropertiesChanging="OnPagePropertiesChanging">
    <ItemTemplate>
        <asp:HyperLink ID="productID" runat="server" NavigateUrl='<%# Eval("product_name", "~/{0}.aspx") %>' CssClass="product-link">
                        <!--blocks-starts--><div class="blocks blocks-shop">
                            <asp:Image ID="readyStock" runat="server" ImageUrl="images/common/ready_stock_tag.png" Visible="false" CssClass="tag" />
                            <asp:Label ID="checkReadyStock" runat="server" Visible="false"></asp:Label>
                        <div class="block-img">
                            <img src='<%# Eval("image") %>' runat="server" id="proImg" />
                        </div>

                        <div class="block-content">
                            <span class="sku" style="font-size:0.6em !important">Item No. <asp:Label ID="skuID" runat="server" Text='<%# Eval("sku") %>'></asp:Label></span>
                            <h3>
                                <asp:Label ID="prodName" runat="server" Text='<%# Eval("product_name") %>'></asp:Label></h3>
                            <p><strong><asp:Label ID="priceRange" runat="server" Text='<%# Eval("price_range") %>'></asp:Label></strong></p>
                        </div>
                   </div><!--blocks-ends-->
                            </asp:HyperLink>
                        </ItemTemplate>

                        <LayoutTemplate>
                            <div id="itemPlaceholderContainer" runat="server" style="">
                                <div runat="server" id="itemPlaceholder" />
                            </div>

                            <div class="datapager">
                                <asp:DataPager ID="DataPager1" ClientIDMode="Static" runat="server" PageSize="18" PagedControlID="products" ViewStateMode="Enabled">
                                    <Fields>
                                        <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="False" ShowNextPageButton="false" ButtonCssClass="nextPre" />
                                        <asp:NumericPagerField ButtonType="Link" ButtonCount="10" />

                                        <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" ButtonCssClass="nextPre" />
                                    </Fields>
                                </asp:DataPager>
                                <div class="clear"></div>
                            </div>
                        </LayoutTemplate>

                        <EmptyDataTemplate>
                            <strong>No Items Found....</strong>
                        </EmptyDataTemplate>
    </ItemTemplate>

    <LayoutTemplate>
        <div id="itemPlaceholderContainer" runat="server" style="">
            <div runat="server" id="itemPlaceholder" />
        </div>

        <div class="datapager">
            <asp:DataPager ID="DataPager1" ClientIDMode="Static" runat="server" PageSize="18" PagedControlID="products" ViewStateMode="Enabled">
                <Fields>
                    <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="False" ShowNextPageButton="false" ButtonCssClass="nextPre" />
                    <asp:NumericPagerField ButtonType="Link" ButtonCount="10" />

                    <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" ButtonCssClass="nextPre" />
                </Fields>
            </asp:DataPager>
            <div class="clear"></div>
        </div>
    </LayoutTemplate>

    <EmptyDataTemplate>
        <strong>No Items Found....</strong>
    </EmptyDataTemplate>
</asp:ListView>

0 个答案:

没有答案