0

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
    {...}

}
codablank1
  • 6,055
  • 5
  • 19
  • 29
  • it works here, are you sure the ItemCommand is binded? The event should bubble by default... – Luizgrs Oct 06 '14 at 12:04
  • Luizgrs : what do you mean by "ItemCommand is binded" ? – codablank1 Oct 06 '14 at 12:08
  • Do you see something like this in your aspx? – Luizgrs Oct 06 '14 at 12:09
  • Yes I have onitemcommand="formView1_ItemCommand", otherwise it wouldn't trigger when I declare classicaly – codablank1 Oct 06 '14 at 12:16
  • Your issue seems to be in somewhere else, I have even create a test site here with only your code, and it works by default... – Luizgrs Oct 06 '14 at 13:25
  • Noticed your `UserControl` addition. How are you instantiating the UserControl - declaratively, or in code? For that matter, are you declaring the `FormView` in the `UserControl` markup, or creating it in code? – Ann L. Oct 06 '14 at 18:25
  • @Ann L. : FormView is declared in aspx and User control instantiated declaratively – codablank1 Oct 07 '14 at 08:34
  • The only other thing I can think of (which I mentioned in one of the edits to my answer) is to make sure that any control you create dynamically has its ID property explicitly set. And, of course, that all of this (user control and all) is inside the Form on your web page. – Ann L. Oct 08 '14 at 12:49

1 Answers1

0

This is a little outside my experience, but I note you don't show your buttons raising events within your template. You don't appear to be handling the events raised by the button commands.

There's nothing that I see to make the buttons cause the template object they live in to raise its ItemCommand event.

Like I said, this is a bit outside my experience, so maybe that's supposed to auto-wire itself. But I'd try handling the buttons' Command event and having them raise the ItemCommand of the template.

ETA: Having done some reading, I think Luizgrs is right and you shouldn't need to do special event handling.

I'm wondering now if the problem is that you are not adding any of your controls to container.Controls until the very end. By doing it that way, you're relying on the Add method of container.Controls to go through the control hierarchy of the Table and hook up all the Command events with BubbleEvent.

Just as an experiment, try moving this line:

 container.Controls.Add(ItemTable);

... right to the top, like this:

 Table ItemTable = new Table();
 ItemTable.CssClass = "tablewidth";
 container.Controls.Add(ItemTable);

The difference would be that all your control additions would now be to controls already inside container.Controls.

ETA: ALSO! You need to assign an ID to the buttons!

    ImageButton ImgBtnfvPrincipalInsertMode = new ImageButton();
    ImgBtnfvPrincipalInsertMode.ID = "ImgBtnfvPrincipalInsertMode";
    ImgBtnfvPrincipalInsertMode.CausesValidation = false;
    ImgBtnfvPrincipalInsertMode.ImageUrl = "~/Images/icon_insert_16.gif";
    ImgBtnfvPrincipalInsertMode.CommandName = "New";
Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • Commands events in ASP.NET bubbles through the hierarchy http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.command(v=vs.110).aspx – Luizgrs Oct 06 '14 at 12:25
  • @Ann L. so you suggest to handle events inside instantiatein ? Could you provide an example ? – codablank1 Oct 06 '14 at 12:27
  • @Luizgrs Having done some reading, I think you're right. I have amended my answer with a different idea. – Ann L. Oct 06 '14 at 13:21
  • @codablank1 Having done some reading, I think Luizgrs is right. I have amended my answer with a different suggestion. – Ann L. Oct 06 '14 at 13:22
  • @codablank1 One more suggestion. I think this is the key difference between what you were doing declaratively and programmatically. – Ann L. Oct 06 '14 at 13:47
  • @Ann L. Tried your last solutions, but still doesn't do it; I really wonder what differs in my code from Luizgrs' – codablank1 Oct 06 '14 at 14:05