单击 gridvioew 索引中的下载按钮超出范围时出现以下错误。必须是非负数且小于集合的大小。参数名称:索引我的aspx代码是
<asp:GridView ID ="gvDetails" runat ="server" AutoGenerateColumns="False" >
<Columns >
<asp:BoundField DataField ="auto_id" ReadOnly ="True" HeaderText="Sr No." />
<asp:BoundField DataField ="topic_name" ReadOnly ="True" HeaderText ="Announcement" />
<asp:TemplateField HeaderText ="File Path">
<ItemTemplate >
<asp:LinkButton ID ="lnkDownload" runat ="server" Text="Download" OnClick="lnkDownload_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
my code for downloading is
protected void lnkDownload_Click(object sender, EventArgs e)
{
try
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int field = Convert.ToInt32(gvDetails.DataKeys[gvrow.RowIndex].Value .ToString ());
SqlDataReader dr = MclsAssignment.getDownload(field);
if (dr.Read())
{
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["mfile_name"] + "\"");
Response.BinaryWrite((byte[])dr["file_data"]);
Response.End();
}
}
catch (Exception)
{
throw;
}
}