Search code examples
c#dynamicdatagridlinkbuttonedititemtemplate

Datagrid EditItemTemplate button event not firing


I am dynamically creating an Item template in a Gridview.

TemplateColumn BtnTmpField = new TemplateColumn(); 
BtnTmpField.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item, "Edit", "Button");  
BtnTmpField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Header, "Edit", "Command");  
BtnTmpField.EditItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.EditItem, "Update", "Button"); dgdefault.Columns.Add(BtnTmpField);

public void InstantiateIn(System.Web.UI.Control Container) 
{ 
    switch (ItemType) 
    { 
        case ListItemType.Header: Literal header_ltrl = new Literal(); 
        header_ltrl.Text = "" + FieldName + ""; 
        Container.Controls.Add(header_ltrl); 
        break; 
        case ListItemType.Item: 
        switch (InfoType) 
        { 
             case "Button": 
             LinkButton edit_button = new LinkButton(); 
             edit_button.ID = "edit_button"; 
             edit_button.Text = "Edit"; 
             edit_button.CommandName = "Edit"; 
             Container.Controls.Add(edit_button); 
             break; 
        }
        break;
        case ListItemType.EditItem: 
             if (InfoType == "Button") 
             { 
             LinkButton update_button = new LinkButton(); 
             update_button.ID = "update_button"; 
             update_button.CommandName = "Update"; 
             update_button.Text = "Update  "; 
             LinkButton cancel_button = new LinkButton(); 
             cancel_button.ID = "cancel_button"; 
             cancel_button.CommandName = "Cancel"; 
             cancel_button.Text = "Cancel"; 
             Container.Controls.Add(update_button); 
             Container.Controls.Add(cancel_button);
            }                 
            break;
      }     
} 

When I select the "Edit" button the "Update" and "Cancel" buttons show up with the selected row editable. The ItemCommand event of the DataGrid fires correctly when "Edit" is clicked. When I click the "Update" or "Cancel" buttons nothing fires. The ItemCommand doesn't fire, and neither does the UpdateCommand or CancelCommand when I explicitly put the onUpdateCommand or onCancelCommand in the ascx page. I can't figure out why nothing is firing when the buttons in the EditItemTemplate is clicked. Also Everything is being loaded on every page_init postback. Any tips would be helpful


Solution

  • I figured it out. I basically created an item template, and added 3 separate buttons without using the edititem. In the data grid itemdatabound function, I just hide and show the buttons that need to be shown.