I have been following the following post on using multiple ItemTemplates in a ListView control.
While following this example does produce output, I am trying ot figure out how to psas an object through to the ItemTemplate's user control, which I do not seem to be able to do/figure out.
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);
}
The User Control is as follows:
public partial class OfficialComment : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
In the example, the values are being output in the ascx page:
<%# Eval("ItemName") %>
however I need to access the ListItem in this control to do other logic. I cannot figure out how I to send through my Comment item. The sender object and EventArgs do not contain the info.
EDIT: Ideally, I would like to obtain an explanation as to how the control accesses the dataitem when using the <%# Eval %> statement. What I have been able to determine, is the following way to gain access to the current item:
I have created a custom ListView control, which set's the dataItemIndex on ItemCreating.
In my Official comment control, I add the following:
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];
}