GridView 自带删除按钮弹出确定按钮的问题

发布时间:2024-05-11 22:08 发布:上海旅游网

问题描述:

我在书上看到了一行代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
}
但是用在我的程序上就崩溃!错误提示是
指定的参数已超出有效值的范围。
参数名: index
请高手指点
我的删除功能可以正常运行的并且用的是GridView自带的删除按钮!

问题解答:

删除按钮在第一列?

另外在if (e.Row.RowType == DataControlRowType.DataRow) 前面加 if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
这段代码

因为你的e.Row.Cells[0]里面没有Control或者有Control但是不是LinkButton,GridView的第一行应该是表头,有也是个lable,在判断里面加上是否含有LinkButton的判断就搞定了

在 GridView1_RowDataBound事件下多加一句:if(e.Item.ItemIndex!=-1)

热点新闻