简体   繁体   中英

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

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.

您是否正在重新绑定网格的PreRender中的事件?

I had the same issue today. I noticed that it was calling the "Header" template for edit items. not sure why, but add the following just before "switch":

System.Diagnostics.Debug.WriteLine(string.Format("Template: {0}",ItemType.ToString()));

if you're testing with a small-ish number of records, you should be able to see that it calls the "header" item type when you attempt to edit the record.

I'd be curious to know if you're experiencing the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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