Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 729979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:51:34+00:00 2026-05-14T06:51:34+00:00

I have been following the following post on using multiple ItemTemplates in a ListView

  • 0

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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:51:34+00:00Added an answer on May 14, 2026 at 6:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have been using the following script for over a year now to
I having trouble in dividing the HTML frames. I have been using the following
Hi I am quite new to php but i have been following some tutorials
Recently I have started playing with jQuery, and have been following a couple of
The following have been proposed for an upcoming C++ project. C++ Coding Standards, by
The rails books and web pages I've been following have all stuck to very
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
I have been searching everywhere for the following functionality in Lisp, and have gotten
People have been developing own solutions to the following problems: Consistent messaging frameworks for
I have been told that there is a performance difference between the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.