Pass object to dynamically loaded ListView ItemTemplate
- by mickyjtwin
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.