2010-02-02 4 views
1

EditItemTemplate 및 InsertItemTemplate이 포함 된 ListView가 있습니다. 이 두 형식은 거의 모든 마크 업을 공유합니다. 예를 들어 :EditItemTemplate 및 InsertItemTemplate에서 동일한 태그가있는 ASP.NET ListView

물론
<asp:listview runat="server" ... > 
    <layouttemplate>...</layouttemplate> 
    <itemtemplate> 
     <p><%#Eval("Name")%></p> 
     <p><%#Eval("Title")%></p> 
     ... 
    </itemtemplate> 
    <insertitemtemplate> 
     <p>Name: <asp:textbox runat=server text='<%#Bind("Name")%>' /></p> 
     <p>Title: <asp:textbox runat=server text='<%#Bind("Title")%>' /></p> 
     ... 
     <asp:button runat=server commandname="Insert" text="Save" /> 
    </insertitemtemplate> 
    <edititemtemplate> 
     <p>Name: <asp:textbox runat=server text='<%#Bind("Name")%>' /></p> 
     <p>Title: <asp:textbox runat=server text='<%#Bind("Title")%>' /></p> 
     ... 
     <asp:button runat=server commandname="Update" text="Save" /> 
    </edititemtemplate> 
</asp:listview> 

는 실제로 삽입 및 편집 템플릿 (서식, 검증 등으로 분야를 많이,) 진행이 많이있다, 그리고 나는 같은 마크 업을 유지하기가 싫어 두번.

<insertitemtemplate> 
     <custom:myform runat=server /> 
     <asp:button runat=server commandname="Insert" text="Save" /> 
    </insertitemtemplate> 
    <edititemtemplate> 
     <custom:myform runat=server /> 
     <asp:button runat=server commandname="Update" text="Save" /> 
    </edititemtemplate> 

불행하게도, 양방향 바인딩 (텍스트 = '<퍼센트 번호 바인드 ("푸") :

내 첫번째 생각은 사용자 컨트롤 (의 .ascx)에 모든 공유 마크 업을 이동했다 %> ')는 폼이 사용자 정의 컨트롤에있을 때만 한 방향으로 작동합니다 (컨트롤의 데이터를 다시 데이터베이스에 유지하지 않음).

다른 대안은 모든 공유 마크 업을 포함 파일로 이동하는 것입니다. 서버 측 포함은 고전적인 ASP에 대한 후퇴이지만 ASP.NET에서 여전히 작동하며 이와 같은 상황에서 유용 할 수 있습니다. 포함 파일의 내용이 페이지에 표시된 것과 같이 취급되기 때문입니다.

그러나 포함 파일은 여전히 ​​조금 혼란스럽고 단점이 있습니다 (예 : VisualStudio는 그다지 익숙하지 않습니다). 대안이 있습니까?

+0

이 게시물에 따르면 : 'http : // forums.asp.net/p/1344635/2729853.aspx', 선언적으로하는 것은 매우 까다 롭습니다. 코드 숨김에서 템플릿을 만드는 것이 더 쉬울 것입니다. – keyboardP

답변

2

필자는 사용자 정의 ListView를 작성하여 직선적으로 만들었습니다. 더 InsertItemTemplate이없는 경우, EditItemTemplate는 모두 사용됩니다

:

Private Sub ListView_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init 
     If Me.InsertItemTemplate Is Nothing Then 
      Me.InsertItemTemplate = Me.EditItemTemplate 
     End If 
    End Sub 

는 또한 "업데이트"적절하게 "삽입"사이의 명령 이름을 전환 버튼 "저장"사용자 정의를 만들었습니다

Private Sub SaveLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click 
     Dim _ListView As Controls.ListView = GetListView() 
     If _ListView IsNot Nothing Then 
      If Me.BindingContainer Is _ListView.EditItem Then 
       Me.CommandName = "Update" 
      Else 
       Me.CommandName = "Insert" 
      End If 
     End If 
    End Sub 

그것 뿐이다

을 (. 위의 GetListView 기능은 그냥 ListView를 찾을 때까지 버튼의 부모를 산책) -이 누군가에게 도움이되기를 바랍니다.

1

나는 매우 늦게 파티에 해요,하지만 선언적 솔루션을 찾고 사람을 위해, 내가하고 결국 다음 (control는 내 FormView) :

:
if (control.EditItemTemplate == null) 
{ 
    control.EditItemTemplate = control.InsertItemTemplate; 
} 

그리고 템플릿

<InsertItemTemplate> 

    ... template ... 

    <asp:LinkButton Text="Insert" CommandName="Insert" runat="server" 
        Visible='<%# Container.ItemType == ListViewItemType.InsertItem %>' /> 
    <asp:LinkButton Text="Update" CommandName="Update" runat="server" 
        Visible='<%# Container.ItemType == ListViewItemType.DataItem %>' /> 
    <asp:LinkButton Text="Cancel" CommandName="Cancel" runat="server" 
        Visible='<%# Container.ItemType == ListViewItemType.DataItem %>' /> 
</InsertItemTemplate> 

재미있는 비트는 분명히 : Container.ItemType == ListViewItemType.DataItem (및 기타)입니다. 이렇게하면 템플릿 유형에 따라 버튼의 가시성이 올바르게 설정됩니다.