事件没有抓到

问题描述:

我实现了一个新的动态的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 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> 

然后它可以工作...

您建议哪种解决方案?

编辑

忘了提FormView控件实际上是包裹用户控件中:

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

    private sealed class CustomItemTemplate : ITemplate 
    {...} 

} 
+0

它在这里工作,你确定ItemCommand是绑定的吗?事件应该默认冒泡... – Luizgrs 2014-10-06 12:04:41

+0

Luizgrs:“ItemCommand绑定”是什么意思? – codablank1 2014-10-06 12:08:36

+0

你在aspx中看到过这样的东西吗? Luizgrs 2014-10-06 12:09:54

这是一个小我的经验之外,但我注意到你不显示你的按钮在您的模板中引发事件。您似乎没有处理按钮命令引发的事件。

没有什么我看到使按钮导致他们居住的模板对象来提高它的事件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里面的控件了。

ETA:ALSO!您需要为按钮指定一个ID!

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

ASP.NET中的命令事件通过层次结构起泡气泡http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.button.command(v=vs.110).aspx – Luizgrs 2014-10-06 12:25:54

+0

@Ann L.所以你建议处理instantiatein中的事件?你能提供一个例子吗? – codablank1 2014-10-06 12:27:44

+0

@Luizgrs在完成了一些阅读之后,我认为你是对的。我用一个不同的想法修改了我的答案。 – 2014-10-06 13:21:45