0

次のような新しい動的 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);
    }
  }

これには 2 つのボタンがあり、最初のボタンで挿入モードが開き、2 つ目のボタンで更新モードが開きます。問題なく表示されます。

私の目標は、フォームビューで使用することです:

protected void Page_Load(object sender, EventArgs e)
{
   formView1.ItemTemplate = new CustomItemTemplate();
}

そして、2つのボタンからコマンドをキャッチしたいと思います:

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

}
4

1 に答える 1

0

これは私の経験から少し外れていますが、テンプレート内でイベントを発生させるボタンが表示されていないことに注意してください。ボタンコマンドによって発生したイベントを処理していないようです。

ItemCommandボタンが存在するテンプレートオブジェクトがそのイベント を発生させる原因となるものは何もありません。

私が言ったように、これは私の経験から少し外れているので、おそらくそれ自体が自動配線されるはずです。しかし、ボタンのCommandイベントを処理してItemCommand、テンプレートの を発生させてみます。

ETA: いくつか読んだ結果、Luizgrs は正しく、特別なイベント処理を行う必要はないと思います。

最後までコントロールを追加していないことが問題なのかどうか疑問に思っていますcontainer.Controls。そのようにすることで、 の制御階層を通過し、 ですべてのコマンド イベントを接続するのAddメソッドに依存しています。container.ControlsTableBubbleEvent

実験として、次の行を移動してみてください。

 container.Controls.Add(ItemTable);

...次のように、一番上に:

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

違いは、すべてのコントロールの追加が、既に 内にあるコントロールに追加されることcontainer.Controlsです。

ETA: また!ボタンに ID を割り当てる必要があります。

    ImageButton ImgBtnfvPrincipalInsertMode = new ImageButton();
    ImgBtnfvPrincipalInsertMode.ID = "ImgBtnfvPrincipalInsertMode";
    ImgBtnfvPrincipalInsertMode.CausesValidation = false;
    ImgBtnfvPrincipalInsertMode.ImageUrl = "~/Images/icon_insert_16.gif";
    ImgBtnfvPrincipalInsertMode.CommandName = "New";
于 2014-10-06T12:22:21.620 に答える