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

DataGrid中动态添加的按钮列如何添加属性(如"onclick")
DataGrid中动态创建了一按钮列,代码如下:
    ButtonColumn   delBC=new   ButtonColumn();
    delBC.Text= "删除 ";
    delBC.CommandName= "Delete ";
    delBC.HeaderStyle.Width=50;
    this.dgData.Columns.Add(delBC);
添加成功,但在dgData_ItemDataBound方法中添加属性时:
((LinkButton)e.Item.Cells[e.Item.Cells.Count-1].Controls[0]).Attributes.Add( "onclick ", "javascript:return   confirm( '确定删除吗? ') ");
提示出错: "指定的参数已超出有效值的范围 ".(注:如果非动态添加的按钮列,无此问题)
请问如何解决?

------解决方案--------------------
if (e.Row.RowType == DataControlRowType.DataRow)//为删除按钮添加提示
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
LinkButton lb = (LinkButton)e.Row.FindControl( "LinkButton1 ");
((LinkButton)e.Row.FindControl( "LinkButton1 ")).Attributes.Add( "onclick ", "javascript:return confirm( '你确认要删除:\ " " + e.Row.Cells[2].Text + "\ "吗? ') ");
}
}


类似这个 你找一下原因 没有问题的

------解决方案--------------------
up