0

单击 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;
}
}
4

4 回答 4

0
<asp:LinkButton ID ="lnkDownload" runat ="server" Text="Download" RowIndex='<%# Container.DisplayIndex %>' OnClick="lnkDownload_Click" ></asp:LinkButton>


---------------------------------------------------------------
int field = Convert.ToInt32(lnkbtn.Attributes["RowIndex"]);

希望这会让您获得 rowIndex 但是,如果您遇到问题,请详细说明您的问题并准确说明您在哪里遇到错误?

但是使用 Command 事件会更高效。

于 2013-08-12T08:59:17.663 回答
0

请尝试 Row Command 事件,这是您问题的最简单解决方案

例子

<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"   CommandName="Download"  CommandArgument='<%#Eval("YourID") %>'></asp:LinkButton>
    </ItemTemplate>
  </asp:TemplateField>    
</Columns>
</asp:GridView>




protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.ToLower() == "application")
    {

          // write your code
    }
}
于 2013-08-12T11:30:37.730 回答
0

当未选择任何行时,您将获得异常。因此,您需要检查

gvrow.RowIndex

大于-1。

protected void lnkDownload_Click(object sender, EventArgs e)
{
  try
  {
    if(gvrow.RowIndex < 0)   //return if user hasn't selected any row
        return;
    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;
   }
}
于 2013-08-12T08:48:57.877 回答
0

我认为您必须检查 gvRow.RowIndex 是否为-1。如果用户没有在 gridview 中选择一行,它会给你异常。

于 2013-08-12T08:49:48.833 回答