日期:2014-05-18  浏览次数:20745 次

但点击编辑时出现System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围的错误.
protected   void   GridView1_RowDataBound(objectsender,GridViewRowEventArgs   e)
        {
                if   (e.Row.RowType   ==   DataControlRowType   .DataRow     )
                {
                  ((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add( "onclick ",   "return   confirm( '你确认删除吗? '); ");
                    ((LinkButton)(e.Row.Cells[7].Controls[0])).Attributes.Add( "onclick ",   "return   confirm( '你确认编辑吗? '); ");
                   
                }
          }
但点击编辑时出现System.ArgumentOutOfRangeException:   指定的参数已超出有效值的范围的错误.
要是把((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add( "onclick ",   "return   confirm( '你确认删除吗? '); ");注释起来就可以编辑了.
这是为什么呀?????????????//
请高手指点.


------解决方案--------------------
建议不要使用索引定位: e.Row.Cells[6].Controls[0],只要你的列一增删改,改索引就要同步更新,很试麻烦

应该使用 FindControl 方法,不容易出错

LinkButton lnk = e.Row.FindControl( "要查找的LinkButton的ID ") as LinkButton;
lnk.Attributes.Add( "onclick ", "return confirm( '你确认删除吗? '); ");