2014-10-06 59 views
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); 
    } 
    } 

它包含兩個按鈕,第一個打開插入模式,第二個打開更新模式。他們顯示沒有問題。

我的目標是在一個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

回答

0

這是一個小我的經驗之外,但我注意到你不顯示你的按鈕在您的模板中引發事件。您似乎沒有處理按鈕命令引發的事件。

沒有什麼我看到使按鈕導致他們居住的模板對象來提高它的事件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