(C#)GridView一个奇怪的问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 05:51:15
在Gridview控件中,我按编辑按钮的时候抛出异常
(指定的参数已超出有效值的范围。参数名: index )
出错在这段代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) //(这个绑定删除按钮的)
{
if (e.Row.RowIndex>= 0)
{
LinkButton lbtnDel = new LinkButton();
lbtnDel = (LinkButton)e.Row.Cells[6].Controls[0];
lbtnDel.Attributes.Add("onclick", "return confirm('你确认删除吗?')");
}
}
但是我按删除按钮的时候,它会提示“你确认删除吗”,说明这段代码也没错呀,可是我注释掉这段代码后,按编辑就不会抛出异常了,
请问是什么问题? 哪个高手指点一下,谢谢

protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow && this.gvUser.EditIndex != e.Row.DataItemIndex)
{
((LinkButton)(e.Row.Cells[0].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除吗?');");
}
}
catch (Exception error)
{ }
}