sword
sword

Reputation: 13

ASP.NET gridview dynamically added columns issue

I've got simple gridview below, where columns are generated dynamically on page load when not post back.

<asp:GridView runat="server" ID="gridMain"
 CssClass="table table-striped table-bordered"
 OnRowCommand="gridMain_onRowCommand"
 OnRowDataBound="gridMain_onRowDataBound"
 OnRowEditing="gridMain_onRowEditing"
 OnRowUpdating="gridMain_onRowUpdating"
 OnRowCancelingEdit="gridMain_onRowCancelingEdit"
 AutoGenerateColumns="False"
 DataKeyNames="StudentId">
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var actionField = new TemplateField
            {
              HeaderText = "Action",
              ItemTemplate = new BtnEditTemplate(),
              EditItemTemplate = new BtnUpdateTemplate()
            };
            gridMain.Columns.Add(nameField);
        }
     }

Here is BtnEditTemplate()

public class BtnEditTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        var btnEdit = new LinkButton {ID = "btnEdit" CommandName = "Delete"};
        container.Controls.Add(btnEdit);
    }
}

The problem is that gridMain_onRowCommand event is not firing in this case. The only one event that works is page load. Can anyone help me?

Upvotes: 0

Views: 76

Answers (1)

Elim Garak
Elim Garak

Reputation: 1827

You have your code in the following method. Did you create this one?

protected void gridMain_onRowCommand(object sender, GridViewCommandEventArgs e)
{

}

Upvotes: 0

Related Questions