我在 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;
}
}
当我选择“编辑”按钮时,“更新”和“取消”按钮会显示所选行可编辑。单击“编辑”时,DataGrid 的 ItemCommand 事件会正确触发。当我单击“更新”或“取消”按钮时,没有任何反应。当我明确地将 onUpdateCommand 或 onCancelCommand 放在 ascx 页面中时,ItemCommand 不会触发,UpdateCommand 或 CancelCommand 也不会触发。我无法弄清楚为什么单击 EditItemTemplate 中的按钮时什么都没有触发。此外,每个 page_init 回发都正在加载所有内容。任何提示都会有所帮助