我實現了一個新的動態的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
{...}
}
它在這裏工作,你確定ItemCommand是綁定的嗎?事件應該默認冒泡... – Luizgrs 2014-10-06 12:04:41
Luizgrs:「ItemCommand綁定」是什麼意思? – codablank1 2014-10-06 12:08:36
你在aspx中看到過這樣的東西嗎? –
Luizgrs
2014-10-06 12:09:54