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];
}