views:

42

answers:

1

I have a static method that takes a parameter and returns a class. the class has a ReadOnlyCollection Property that i'd like to display in a asp:repeater. Is there a way to do this using LinqDataSource or ObjectDataSource? I got pretty close with ObjectDataSource, but since the method is returning a single class object, I couldn't get me repeater to bind to the Property.. Here's what I did:

ClassName: ClassName StaticMethod: StaticMethod(ParamName) ReadOnlyCollection: ClassName.Collection

<asp:objectdatasource 
  runat="server" 
  id="myData"
  selectmethod="StaticMethod"
  typename="ClassName"
>
  <selectparameters>
    <asp:parameter name="ParamName" defaultvalue="Value" />
  </selectparameters>
</asp:objectdatasource>

<asp:repeater runat="server" datasourceid="myData">
  <itemtemplate>
    <%# Container.DataItem %>
  </itemtemplate>
</asp:repeater>

So, This only returns the readonly collection object, not each item as i'd like. Is this possible without have actual code to instantiate the object?

A: 

If you're only concerned with displaying data in your collection you could add another static method that returns your objects ReadOnlyCollection and bind to that. Alternatively you can ditch the ObjectDataSource and do it in code, something like:

    var myObj = ClassName.StaticMethod(someParam);
    MyRepeater.DataSource = myObj.Collection;
    MyRepeater.DataBind();

If you need to display both data in your class and collection, then you can add another Repeater to the ItemTemplate:

<asp:objectdatasource runat="server" id="myData" 
 selectmethod="StaticMethod" typename="ClassName">
    <selectparameters>
        <asp:parameter name="ParamName" defaultvalue="Value" />
    </selectparameters>
</asp:objectdatasource>

<asp:Repeater runat="server" datasourceid="myData" OnItemDataBound="Rep_ItemDataBound">
    <ItemTemplate>
        <%# Eval("SomeProperty") %>
        <ul>
            <asp:Repeater id="RepCollection" runat="server">
                <ItemTemplate>
                    <li><%# Eval("SomeCollectionProperty") %></li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
    </ItemTemplate>
</asp:Repeater>

The ItemDataBound method would look something like:

protected void Rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        var dataItem = (ClassName)e.Item.DataItem;
        var innerRepeater = (Repeater)e.Item.FindControl("RepCollection");
        innerRepeater.DataSource = dataItem.Collection;
        innerRepeater.DataBind();
    }
}
richeym
well, I need the data from the ClassName object too... Is there a way to make another datasource based on the property from the existing datasource?
Rob
You can nest Repeaters quite easily. So what you could do is add a child Repeater for your ReadOnlyCollection, then bind to it programatically in the outer controls 'ItemDataBound' event.
richeym
Rats.. okay.. that's what i feared.. Okay..
Rob