【发布时间】: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