2010-04-16 7 views
0

ListView 컨트롤에서 여러 ItemTemplates를 사용하는 경우 다음 게시물을 수행하고 있습니다.동적으로로드 된 ListView ItemTemplate에 객체 전달

이 예제를 따르는 동안 출력을 생성하는 동안 ItemTemplate의 사용자 정의 컨트롤을 통해 개체를 psas하는 방법을 알아 내려고 노력하고 있는데,이 작업은 수행 할 수없는 것처럼 보입니다.

protected void lvwComments_OnItemCreated(object sender, ListViewItemEventArgs e) 
    { 
     ListViewDataItem currentItem = (e.Item as ListViewDataItem); 
     Comment comment = (Comment)currentItem.DataItem; 

     if (comment == null) 
      return; 

     string controlPath = string.Empty; 

     switch (comment.Type) 
     { 
      case CommentType.User: 
       controlPath = "~/layouts/controls/General Comment.ascx"; 
       break; 
      case CommentType.Official: 
       controlPath = "~/layouts/controls/Official Comment.ascx"; 
       break; 
     } 
     lvwComments.ItemTemplate = LoadTemplate(Controlpath); 
    } 

다음과 같이 제어하는 ​​사용자 :

<%# Eval("ItemName") %> 

그러나 내가 년을 ListItem에 액세스해야합니다

예에서
public partial class OfficialComment : UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

, 값은 ASCX 페이지에서 출력되는 이 컨트롤은 다른 로직을 수행합니다. 내 코멘트 항목을 통해 보내는 방법을 알아낼 수 없습니다. 보낸 사람 개체와 EventArgs에는 정보가 들어 있지 않습니다.

편집 : <퍼센트 번호 평가 %> 문을 사용하는 경우 컨트롤이있는 DataItem에 액세스하는 방법 이상적으로, 나는에 관한 설명을 취득하고 싶습니다. 내가 결정할 수 있었던 것은 다음과 같은 방법으로 현재 항목에 액세스 할 수 있습니다.

ItemCreating에 dataItemIndex를 설정하는 사용자 지정 ListView 컨트롤을 만들었습니다. 동적 항목 템플릿에 대한 문서가 있지만

List<Comment> commentList = ((CommentListView)this.Parent.Parent.Parent).DataSource as List<Comment>; 

if (commentList != null) 
{ 
    int currentIndex = ((ListViewDataItem)this.Parent).DataItemIndex; 
    Comment currentItem = commentList[currentIndex]; 
} 

답변

0

, 아니 예는 다른 그 <% 번호는 평가 %> 기능을 데이터에 액세스를 통해 진행하지 : 내 공식 코멘트 제어에서

, 나는 다음과 같은 추가합니다. 내 질문에 표시된 몇 가지 방법을 시도한 후에, 나는 재귀 적으로 제어 트리를 따라 다니지 않아도되는 팬이 아니 었습니다.

내가 할 수 있었던 것은 UserControl에서 상속 한 클래스를 만드는 것입니다. 나는 지금 CurrentItem에 따라 내 모든 사용자 지정 논리를 수행 할 수 있습니다 내 템플릿 컨트롤에서

protected void lvwComments_OnItemCreated(object sender, ListViewItemEventArgs e) 
    { 
     ListViewDataItem currentItem = (e.Item as ListViewDataItem); 
     Comment comment = (Comment)currentItem.DataItem; 

     if (comment == null) 
      return; 

     string controlPath = string.Empty; 

     switch (comment.Type) 
     { 
      case CommentType.User: 
       controlPath = "~/layouts/controls/General Comment.ascx"; 
       break; 
      case CommentType.Official: 
       controlPath = "~/layouts/controls/Official Comment.ascx"; 
       break; 
     } 

     ListViewTemplateControl<Comment> templateControl = LoadControl(controlPath) as ListViewTemplateControl<Comment>; 

     if (templateControl != null) 
     { 
      templateControl.CurrentItem = comment; 
      templateControl.ID = comment.ItemID; 
      lvwComments.Controls.Add(templateControl); 
     } 
    } 

:

public partial class ListViewTemplateControl<T> : UserControl where T : class 
{ 
    public T CurrentItem { get; set; } 
} 

그런 다음 내 ListView에, 나는 다음을 수행 할 수 있습니다 :이 클래스는 내있는 DataItem을 정의합니다 (DataItem)을 전달합니다. 이것에 대한 유일한주의 사항은 < % # Eval %> 함수가 작동하지 않는다는 것입니다.

관련 문제