大家好。今天碰到一个稍微奇怪点的问题。
在用GridView的时候,我在rowdatabound里写下下面的代码
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#66ccff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            ((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?');");
        }
    }删除按钮的索引是5,这个我确认过,删除按钮是可以用的。
在rowediting里写了如下代码protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView.EditIndex = e.NewEditIndex;
        this.GridViewBinding();
    }这时候调试,发现上述
((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?');");
报错
如果将其注释掉,运行正常。
但是,这句子本身应该是没有错的。它只是为删除按钮添加弹出确认窗口的功能而已。
报错的内容是:指定的参数已超出有效值的范围。参数名:INDEX疑惑不解,请高手赐教。

解决方案 »

  1.   

    用Cells[5].Controls[0]来定位控件可以说是比较初级的做法,不建议使用,如果你的单元格里有一个其他的html标记,那么你代码的可能就要改为Cells[5].Controls[1]用Cells[5].FindControl("MyLinkButton")
      

  2.   

    当处于edit的时候 ,gridview变会调用edit template的模板,在 cells[5]里面,edit template里面,可能没有这个LINKBUTTON
      

  3.   

    应该再判断状态!!
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#66ccff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                if(e.Row.RowState==DataControlRowState.Edit)
                     ((LinkButton)(e.Row.Cells[5].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?');");
            }
    还有在编辑状态下有几个按钮?再确定是Controls[0]还是Controls[1]