?
Solved

Read Anonymous Type from DataItem?

Posted on 2009-04-09
3
Medium Priority
?
1,361 Views
Last Modified: 2013-11-11
I'm attempting to load a page with a datalist nested into a repeater. What I want to do is create the datasource for the datalist on the itemdatabound event of the repeater. It's working however I need to filter my datasource down based on a value in my repeater's itemtemplate (SubCategory). I would like to read the value from the e.Item.DataItem. SubCategory into a string but for whatever reason, I can't seem to get it, although I can see it in the watch window.

// aspx
<asp:Repeater ID="rptItems" runat="server" OnItemDataBound="rptItems_ItemDataBound">
                    <ItemTemplate>
                        <h2><%# Eval("SubCategory") %> /></h2>
                        <asp:DataList ID="dlSubList" runat="server"
                            RepeatDirection="Vertical" RepeatColumns="4">
                            <HeaderTemplate>
                                <ul>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <li><%# Eval("ListItem")%></li>
                            </ItemTemplate>
                            <FooterTemplate>
                                </ul>
                            </FooterTemplate>
                        </asp:DataList>
                    </ItemTemplate>
                </asp:Repeater>
 
private List<ItemData> m_Items; // populated from db
 
// code for repeater data
rptList.DataSource = (from i in m_Items select new {i.SubCategory}).Distinct();
rptList.DataBind();
                                
protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
        string sSubCategory = e.Item.DataItem ???? How do I get the SubCategory value from the anonymous type? 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataList dl = (DataList)e.Item.FindControl("dlSubList");
            var oList = (from p in m_Items as List<PDFIndexRecord>
                         where p.SubCategory.Equals(sSubCategory)
                         select p).ToList();
            dl.DataSource = oList;
            dl.DataBind();
        }
}

Open in new window

0
Comment
Question by:Turgeson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 24118846
Have you tried just ToString()?  

That is a method of ANY object.
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24128731
As Jensfiederer said:
If you need a string value that should be doable. The watch window tends to display values by calling .ToString on the object. If the value in the watch window is the string you want then you should be able to get it by simply calling ToString.

var item = e.Item.DataItem.ToString();

You'd need to do a null check on the various properties as well.
0
 

Accepted Solution

by:
Turgeson earned 0 total points
ID: 24130233
I tried that but it didn't work like I needed it to.  It did create a non-null object with properties and values but it didn't give me a way to access them in my code. If I put a break in the event handler and run item.SubCategory in the immediate window and it gives me nothing (no error, empty string, null), just nothing. After a lot of trial and error I was able to it to work with this using Reflection:

object oSubCategory = e.Item.DataItem.GetType().GetProperties()[2].GetValue(e.Item.DataItem, null); // 2 = SubCategory

and then use the .ToString() method on the object. Kind of a round about way to a property but it works in the context I need.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question