Link to home
Start Free TrialLog in
Avatar of Turgeson
Turgeson

asked on

Read Anonymous Type from DataItem?

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

Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Have you tried just ToString()?  

That is a method of ANY object.
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.
ASKER CERTIFIED SOLUTION
Avatar of Turgeson
Turgeson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial