I implemented a new dynamic ItemTemplate like this :
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);
}
}
It contains two buttons, the first one opening the Insert mode and the second one opening the update mode. They show up with no problem.
My goal is to use it in a formview :
protected void Page_Load(object sender, EventArgs e)
{
formView1.ItemTemplate = new CustomItemTemplate();
}
And I'd like to catch commands from the two buttons :
protected void formView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
System.Diagnostics.Debug.WriteLine("ITEM COMMANDNAME : " + e.CommandName);
}
Unfortunately, formView1_ItemCommand won't display anything when I click on my buttons
Yet, if I declare ItemTemplate classicaly :
<%@ 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>
Then it works...
Which solution do you suggest ?
EDIT
Forgot to mention the formView is actually wrapped inside a 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
{...}
}