我像这样实现了一个新的动态ItemTemplate:
private sealed class CustomItemTemplate : ITemplate
{
public CustomItemTemplate()
{}
void ITemplate.InstantiateIn(Control container)
{
Table ItemTable = new Table();
ItemTable.CssClass = "tablewidth";
TableRow btnRow = new TableRow();
ItemTable.Rows.Add(btnRow);
TableCell btnCell = new TableCell();
btnCell.CssClass = "bgcolorBlueLight";
btnCell.ColumnSpan = 2;
btnRow.Cells.Add(btnCell);
ImageButton ImgBtnfvPrincipalInsertMode = new ImageButton();
ImgBtnfvPrincipalInsertMode.CausesValidation = false;
ImgBtnfvPrincipalInsertMode.ImageUrl = "~/Images/icon_insert_16.gif";
ImgBtnfvPrincipalInsertMode.CommandName = "New";
ImageButton ImgBtnfvPrincipalUpdateMode = new ImageButton();
ImgBtnfvPrincipalUpdateMode.CausesValidation = false;
ImgBtnfvPrincipalUpdateMode.ImageUrl = "~/Images/icon_edit_16.gif";
ImgBtnfvPrincipalUpdateMode.CommandName = "Edit";
btnCell.Controls.Add(ImgBtnfvPrincipalInsertMode);
btnCell.Controls.Add(ImgBtnfvPrincipalUpdateMode);
container.Controls.Add(ItemTable);
}
}
它包含两个按钮,第一个打开插入模式,第二个打开更新模式。他们出现没有问题。
我的目标是在 formview 中使用它:
protected void Page_Load(object sender, EventArgs e)
{
formView1.ItemTemplate = new CustomItemTemplate();
}
我想从两个按钮中获取命令:
protected void formView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
System.Diagnostics.Debug.WriteLine("ITEM COMMANDNAME : " + e.CommandName);
}
不幸的是,当我单击按钮时,formView1_ItemCommand 不会显示任何内容
然而,如果我声明 ItemTemplate 经典:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProspectsCustomFormView.ascx.cs" Inherits="controls_ProspectsCustomFormView" %>
<asp:FormView ID="formView1" runat="server" OnItemCommand="formView1_ItemCommand">
<ItemTemplate>
<asp:Table ID="ItemTable" runat="server" CssClass="tablewidth">
<asp:TableRow>
<asp:TableCell CssClass="bgcolorBlueLight" ColumnSpan="2">
<asp:ImageButton ID="ImgBtnfvPrincipalInsertMode" runat="server" CommandName="New" CausesValidation="False" ImageUrl="~/Images/icon_insert_16.gif" ToolTip="New"/>
<asp:ImageButton ID="ImgBtnfvPrincipalUpdateMode" runat="server" CommandName="Edit" CausesValidation="False" ImageUrl="~/Images/icon_edit_16.gif" ToolTip="Edit" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:FormView>
然后它工作...
您建议哪种解决方案?
编辑
忘了说 formView 实际上是包裹在 User Control 中的:
public partial class controls_CustomFormView : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
fv.ItemTemplate = new CustomItemTemplate();
}
private sealed class CustomItemTemplate : ITemplate
{...}
}