Listview Datapager - 指数超出范围。必须是非负数且小于集合的大小

时间:2016-08-04 06:02:37

标签: c# asp.net listview

我正在使用Listview来显示数据库中的产品列表。 OnPage Load产品正确加载,但是当我尝试使用datapager分页切换页面时,它会出现此错误

指数超出范围。必须是非负数且小于集合的大小。参数名称:shop.products_ItemDataBound(Object sender,ListViewItemEventArgs e)上System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index)的System.Collections.ArrayList.get_Item(Int32 index)的索引

事情是,如果我使用ItemDataBound,那么我只会收到此错误。其他分页工作正常。

ActiveAdmin.register Skill do

  permit_params :title, :text, :status
  validates :title, length: { maximum: 20 }, presence: true

end

Item DataBound

<asp:ListView ID="products" runat="server" DataKeyNames="ID" OnPagePropertiesChanging="OnPagePropertiesChanging">
                            <ItemTemplate>
                              content
                            </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="12" 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>

DataPager的

private void products_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        try {
            if (e.Item.ItemType == ListViewItemType.DataItem) {
                ListViewDataItem itm = (ListViewDataItem)e.Item;
                string productID = products.DataKeys(itm.DataItemIndex)("ID");
                query = "SELECT stock_status FROM products WHERE ID = '" + productID + "'";
                DataTable dt = this.GetData(query);
                if (dt.Rows.Count > 0) {
                    ((Label)e.Item.FindControl("checkReadyStock")).Text = dt.Rows(0)("stock_status").ToString;

                    if (((Label)e.Item.FindControl("checkReadyStock")).Text == "Ready Stock") {
                        ((Image)e.Item.FindControl("readyStock")).Visible = true;
                    }
                }
            }
        } catch (Exception ex) {
            Response.Write(ex);
        }
    }

1 个答案:

答案 0 :(得分:0)

你能尝试一下吗?你只需要改变&#39; ColumnNameWithID&#39;正确的名字。

protected void products_ItemDataBound1(object sender, ListViewItemEventArgs e)
{
    try
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            string productID = DataBinder.Eval(e.Item.DataItem, "ColumnNameWithID").ToString();
            string query = "SELECT stock_status FROM products WHERE ID = '" + productID + "'";
            DataTable dt = this.GetData(query);
            if (dt.Rows.Count > 0)
            {
                ((Label)e.Item.FindControl("checkReadyStock")).Text = dt.Rows[0][2].ToString();
                if (((Label)e.Item.FindControl("checkReadyStock")).Text == "Ready Stock")
                {
                    ((Image)e.Item.FindControl("readyStock")).Visible = true;
                }
            }
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}