简体   繁体   English

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

[英]Set ASP.Net GridView column to Button field programmatically

I'm generating the columns of a gridview in code which is working fine (I have AutoGenerateColumns="false" set on the page), so I don't have any columns defined in the html markup. 我在代码中生成gridview的列,工作正常(我在页面上设置了AutoGenerateColumns="false" ),所以我没有在html标记中定义任何列。

I would like to make one of the columns a ButtonField, so I can use a RowCommand handler in the code - is there a way to make a column a ButtonField programmatically? 我想将其中一个列设为ButtonField,因此我可以在代码中使用RowCommand处理程序 - 有没有办法以编程方式将列设为ButtonField?

ButtonField programmatic adding: ButtonField程序化添加:

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

Markup: 标记:

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

Handler: 处理器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM