2014-10-06 2 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을 클래식으로 선언하면 다음과 같이됩니다.

<%@ 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

+0

Luizgrs : "ItemCommand가 바인드 됨"은 무엇을 의미합니까? – codablank1

+0

당신은 당신의 aspx 에이 같은 것을 볼 수 있습니까? 예 : onitemcommand = "formView1_ItemCommand"예. classicaly를 선언 할 때 트리거되지 않습니다. – Luizgrs

답변

0

,하지만 난 당신이 당신의 버튼이 표시되지 않습니다주의 템플릿 내에서 이벤트를 발생시킵니다. 단추 명령으로 발생한 이벤트를 처리하는 것으로 보이지 않습니다.

단추를 사용하여 서식 파일 개체가 ItemCommand 이벤트를 발생 시키도록 만드는 것은 아무것도 없습니다.

내가 말했듯이 이것은 내 경험 밖의 비트이므로 자동 배선을해야 할 수도 있습니다. 그러나 나는 버튼의 Command 이벤트를 처리하고 템플릿을 ItemCommand 올리도록했습니다.

ETA : 일부 독서를 마친 Luizgrs가 옳다고 생각하고 특별한 이벤트 처리를하지 않아도됩니다.

끝까지 container.Controls에 컨트롤을 추가하지 않는 것이 문제가되는지 궁금합니다. 이렇게하면 메서드가 container.Controls이고 컨트롤 계층 구조가 Table이고 모든 명령 이벤트가 BubbleEvent이됩니다. 다음과 같이 오른쪽 상단에 ...

container.Controls.Add(ItemTable); 

:

다만 실험으로,이 라인을 이동하려고

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

의 차이 일 것입니다 모든 컨트롤 추가 이제 것 이미 container.Controls 안에있는 컨트롤에 있습니다.

도착 일 : 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/en-us/library/system.web.ui.webcontrols.button.command(v=vs.110).aspx – Luizgrs

+0

@Ann L. instantiatein에서 이벤트를 처리하도록 제안 했습니까? 예제를 제공해 줄 수 있습니까? – codablank1

+0

@Luizgrs 약간의 독서를 한 후에, 나는 당신이 옳다고 생각합니다. 나는 다른 생각으로 나의 대답을 수정했다. –