【问题标题】:Listview Datapager - Index was out of range. Must be non-negative and less than the size of the collectionListview Datapager - 索引超出范围。必须是非负数且小于集合的大小
【发布时间】:2016-08-04 06:02:37
【问题描述】:

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

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

问题是如果我使用 ItemDataBound 那么只有我得到这个错误。其他分页效果很好。

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

项目数据绑定

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

数据页面

protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    (products.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    products.DataSource = ViewState("Data");
    products.DataBind();
}

【问题讨论】:

  • products_ItemDataBound中有多个错误。你应该先解决这些问题。
  • @VDWWD 什么错误?
  • 当我将 products_ItemDataBound 中的代码复制到 VS 2015 中时,会出现 5 个错误。
  • @VDWWD 可能是其他一些问题,因为它工作得非常好。你可以看到现场直播brandstik.in/Music。 ItemDataBound 基本上是为了让图像显示 Ready Stock ,它会检查 condition 。如果条件为真,则仅在此处显示 Ready Stock 图像。查看链接您会得到正确的想法。

标签: c# asp.net listview


【解决方案1】:

你可以试试这个。您只需将“ColumnNameWithID”更改为正确的名称。

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

【讨论】:

  • 如果你的意思是我应该把列的名称作为主键,那么 ID 就是我已经做过的那一列的名称
  • 是的,没错。为了这个例子,我使用了一个更易读的列名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多