Pass object to dynamically loaded ListView ItemTemplate

Posted by mickyjtwin on Stack Overflow See other posts from Stack Overflow or by mickyjtwin
Published on 2010-04-16T06:30:44Z Indexed on 2010/04/16 6:33 UTC
Read the original article Hit count: 810

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET