【问题标题】:Event from dynamic ItemTemplate not caught未捕获来自动态 ItemTemplate 的事件
【发布时间】:2014-10-06 11:01:42
【问题描述】:

我像这样实现了一个新的动态 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);
    }
  }

它包含两个按钮,第一个打开插入模式,第二个打开更新模式。他们毫无问题地出现。

我的目标是在表单视图中使用它:

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 实际上是包裹在一个用户控件中的:

public partial class controls_CustomFormView : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       fv.ItemTemplate = new CustomItemTemplate();
    }

    private sealed class CustomItemTemplate : ITemplate
    {...}

}

【问题讨论】:

  • 它在这里工作,你确定 ItemCommand 被绑定了吗?默认情况下,该事件应该冒泡...
  • Luizgrs:“ItemCommand 已绑定”是什么意思?
  • 你在你的aspx中看到过这样的东西吗? onitemcommand="formView1_ItemCommand" onmodechang="formView1_ModeChanging">
  • 是的,我有onitemcommand="formView1_ItemCommand",否则当我经典声明时它不会触发
  • 您的问题似乎出在其他地方,我什至在这里仅使用您的代码创建了一个测试站点,并且默认情况下可以使用...

标签: asp.net formview itemtemplate itemcommand


【解决方案1】:

这有点超出我的经验,但我注意到您没有在模板中显示您的按钮引发事件。您似乎没有处理按钮命令引发的事件。

我没有看到使按钮导致它们所在的模板对象引发其ItemCommand 事件。

就像我说的,这有点超出我的经验,所以也许这应该是自动接线的。但我会尝试处理按钮的Command 事件并让它们引发模板的ItemCommand

ETA:经过阅读,我认为 Luizgrs 是对的,您不需要进行特殊的事件处理。

我现在想知道问题是否在于您直到最后才将任何控件添加到container.Controls。通过这种方式,您将依赖container.ControlsAdd 方法来遍历Table 的控制层次结构并将所有Command 事件与BubbleEvent 挂钩。

作为一个实验,试着移动这条线:

 container.Controls.Add(ItemTable);

...一直到顶部,像这样:

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

不同之处在于您添加的所有控件现在都将添加到 container.Controls 中的控件。

预计到达时间:还有!您需要为按钮分配一个 ID!

    ImageButton ImgBtnfvPrincipalInsertMode = new ImageButton();
    ImgBtnfvPrincipalInsertMode.ID = "ImgBtnfvPrincipalInsertMode";
    ImgBtnfvPrincipalInsertMode.CausesValidation = false;
    ImgBtnfvPrincipalInsertMode.ImageUrl = "~/Images/icon_insert_16.gif";
    ImgBtnfvPrincipalInsertMode.CommandName = "New";

【讨论】:

  • ASP.NET 中的命令事件在层次结构中冒泡msdn.microsoft.com/en-us/library/…
  • @Ann L. 所以你建议在 instantiatein 中处理事件?你能举个例子吗?
  • @Luizgrs 做了一些阅读,我认为你是对的。我用不同的想法修改了我的答案。
  • @codablank1 阅读了一些资料后,我认为 Luizgrs 是对的。我用不同的建议修改了我的答案。
  • @codablank1 还有一个建议。我认为这是您以声明方式和以编程方式执行的操作之间的主要区别。
猜你喜欢
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
相关资源
最近更新 更多