以编程方式将ASP.Net GridView列设置为按钮字段

3
我正在用代码生成网格视图的列,这个过程很顺利(我在页面上设置了AutoGenerateColumns="false"),所以我没有在html标记中定义任何列。
我想让其中一列成为ButtonField,这样我就可以在代码中使用RowCommand处理程序 - 有没有办法通过编程使一列成为ButtonField?
1个回答

5

通过编程方式添加ButtonField:

var buttonField = new ButtonField
                      {
                          ButtonType = ButtonType.Button,
                          Text = "My button",
                          CommandName = "DoSomething",
                      };
Grid.Columns.Add(buttonField);

标记语言:

<asp:GridView runat="server" ID="Grid" AutoGenerateColumns="false" OnRowCommand="RowCommandHandler"></asp:GridView>

处理程序:

protected void RowCommandHandler(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DoSomething")
    {
        // place code here
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接