2017-04-07 64 views
0

简单的问题,我的搜索似乎是空白。使GridView AutGenerateColumn发生RowBmand触发RowCommand事件

我有一个GridView即AutoGeneratingColumns=True

这产生一列 “MyColumn”。我想让这个列的值是一个可点击的LinkBut​​ton,它使用特定的CommandNmae触发GridView的RowCommand事件,并将单元格值作为CommandArgument。

我想要做到这一点,而不使用自定义的ItemTemplate,我知道该怎么做。我希望我可以实际修改GridView中自动生成的列,如上所述。

蒂亚

回答

0

这可以从OnRowDataBound事件GridView控件来完成。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the current row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ///create a new linkbutton 
     LinkButton button = new LinkButton(); 
     button.Text = "LinkButton"; 
     button.CommandArgument = "MyArgs"; 

     //assign a command to the button 
     button.Command += Button_Command; 

     //add the button to cell x 
     e.Row.Cells[3].Controls.Add(button); 
    } 
} 

因为你动态地添加控制,在GridView的结合应该不为IsPostBack检查里面包裹着。你通常会做的事。

+0

我如何处理这个如果我的GridView是绑定在一个按钮甚至点击(在前一个屏幕上)? – Nugs

+0

您必须在某处“存储”点击,就像在会话中一样。因此,当页面重新加载时,您可以执行与按钮单击相同的代码。 – VDWWD