views:

111

answers:

2

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

Are you able to find it using lvwComments.FindControl("putListItemIdHere") ?

Daniel Dyson
A: 

While there is documentation on dynamic Item Templates, no examples progressed through to accessing data other that the <%# Eval %> function. After trying some methods, shown in my question, I was not a fan of having to recursively traverse up the control tree.

What I've been able to do is create a class that inherits from UserControl. This class will define my DataItem:

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

Then, in my ListView, I can perform the following:

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

In my template control, I can now perform all my custom logic based on the CurrentItem (DataItem) that I pass through. The only caveat to this is that <%# Eval %> function will not work now.

mickyjtwin