【问题标题】:Index was out of range. Must be non-negative and less than the size of the collection VB.net Gridview指数超出范围。必须是非负数且小于集合 VB.net Gridview 的大小
【发布时间】:2014-10-20 10:05:17
【问题描述】:

我在 Visual Studio 中有一个 gridview,其中一个列根据插入该行的文件类型显示小图标徽标, 我已经创建了一些代码,我相信一旦单击图标就会打开附加的文件。

当我运行代码时,我得到这个错误:

索引超出范围。必须是非负数且小于集合的大小。参数(名称):索引()。 第 530 行:ImgBtn.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History.DataKeys(e.Row.RowIndex).Value.ToString + "');")

我无法解决这个问题。

这是我在 ASPX 中的 Gridview 代码:

                                 <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="PdfExtention2" runat="server" ImageUrl="../icons/pdf.gif" Visible="false"/>
                            <asp:ImageButton ID="WordExtention2" runat="server" ImageUrl="../icons/actn103.gif" Visible="false"/>
                            <asp:ImageButton ID="ExcelExtention2" runat="server" ImageUrl="../icons/actn102.gif" Visible="false"/>
                        </ItemTemplate>
                        <ItemStyle Width="25%" />
                    </asp:TemplateField>

这是我在 VB 中的 Gridview RowDataBound 代码:

        Dim pdfExtention2, wordExtention2, excelExtention2 As ImageButton

        pdfExtention2 = e.Row.FindControl("PdfExtention2")
        wordExtention2 = e.Row.FindControl("WordExtention2")
        excelExtention2 = e.Row.FindControl("ExcelExtention2")

        If e.Row.DataItem("Extention").ToString = "application/pdf" Then
            pdfExtention2.Visible = True
        ElseIf e.Row.DataItem("Extention").ToString = "application/msword" Then
            wordExtention2.Visible = True
        ElseIf e.Row.DataItem("Extention").ToString = "application/vnd.ms-excel" Then
            excelExtention2.Visible = True
        End If

        '-----------------------------------------------------------------

        Dim ImgBtn As New ImageButton
        ImgBtn = e.Row.FindControl("PdfExtention2")
        ImgBtn.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History.DataKeys(e.Row.RowIndex).Value.ToString + "');")

        Dim ImgBtn2 As New ImageButton
        ImgBtn2 = e.Row.FindControl("WordExtention2")
        ImgBtn2.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History.DataKeys(e.Row.RowIndex).Value.ToString + "');")

        Dim ImgBtn3 As New ImageButton
        ImgBtn3 = e.Row.FindControl("ExcelExtention2")
        ImgBtn3.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History.DataKeys(e.Row.RowIndex).Value.ToString + "');")

我希望我能在哪里出错时得到一些帮助。 提前谢谢你

【问题讨论】:

  • 绑定前是否在标记或代码中设置了 DataKeys 属性?
  • 我刚刚在我的标记中实现了数据键,它现在可以工作了!干杯。 @史蒂夫

标签: asp.net vb.net visual-studio gridview


【解决方案1】:

我的问题解决了!

我需要说:

DataKeyNames="ServiceID,ID"

在 gridview 的标记中,现在可以正常工作了。

我会在 2 天后解决这个问题。

【讨论】:

    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多