//自动生成一个按钮
ButtonColumn bc = new ButtonColumn();
bc.Text = "提交";
bc.CommandName = "tijiao";
DataGrid1.Columns.Add(bc);//注册触发事件
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);//执行事件
DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
////?不执行这个DataGrid1_ItemCommand();
if(e.CommandName == "tijiao")
{
Label1.Text = e.Item.Cells[3].Text.ToString();
}
}

解决方案 »

  1.   

    this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);加到InitializeComponent()中去
      

  2.   

    ButtonColumn bc = new ButtonColumn();
    bc.Text = "提交";
    bc.CommandName = "tijiao";
    DataGrid1.Columns.Add(bc);
    不要加在if(!IsPostBack)内应该没有什么问题啊
    http://www.cnblogs.com/lovecherry/archive/2005/03/25/125526.html
      

  3.   

    你的datagrid是new的还是从页面拖动的?如果是页面拖出来,动态添加按钮是不触发事件的。
      

  4.   

    我的datagrid是拖出来的,总共3个字段,前两列是绑定的数据,最后一列是生成的按忸。我现在的按忸事件不触发,只是回到原来的那两列绑定数据。请问我该怎么实现呢。
      

  5.   

    我遇到这种情况后,datagrid就是用new的方式。
    想你这种需求,可不可以添加好按钮列,当他不需要的时候,设置其visible=false。