错误-索引超出范围.必须为非负数并且小于集合的大小. [英] Error - Index was out of range. Must be non-negative and less than the size of the collection.

查看:72
本文介绍了错误-索引超出范围.必须为非负数并且小于集合的大小.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此错误索引超出范围.必须为非负数并且小于集合的大小.在下面给出的行中

GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;


参数名称:index

我的CS代码在下面给出

Why this error Index was out of range. Must be non-negative and less than the size of the collection. in the line given below

GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;


Parameter name: index

my cs code is given below

protected void lkb1_Click(object sender, EventArgs e)
    {
        LinkButton lnkbtn = sender as LinkButton;
        GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        string filePath = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
        Response.ContentType = "image/jpg";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
        Response.TransmitFile(Server.MapPath(filePath));
        Response.End();
}

推荐答案

GridView1.DataKeys[gvrow.RowIndex].Value.ToString();

给定的RowIndex似乎不存在DataKey.
通过代码进行调试将帮助您了解可能出了什么问题.
GridView1.DataKeys[gvrow.RowIndex].Value.ToString();

A DataKey does not seem to exist for the given RowIndex.
Debugging through your code will help you understand what could be going wrong.


尝试执行此操作以进行运行时调试":
试试
{
字符串filePath = GridView1.DataKeys [gvrow.RowIndex] .Value.ToString();
}
捕获(异常排除)
{
MessageBox.Show(exc.Message);
}
try this for a "runtime debug":
try
{
string filePath = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}


这篇关于错误-索引超出范围.必须为非负数并且小于集合的大小.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆