DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Repeater with Object as Value (http://www.daniweb.com/forums/thread30549.html)

Lacuna Aug 15th, 2005 6:13 am
Repeater with Object as Value
 
I am trying to iterate through a hashtable that i have populated with objects using the repeater control.
For each iteration i wish to output the values of properties of the objects I am populating the hashtable with.


For example: -

 

Class SomeClass
{
public String myProperty1
public String myProperty2

    public SomeClass()
    {
        this.myProperty1 = "hello";
        this.myProperty2 = "goodbye";
    }
}


Class TestSome
{
    public Hashtable myHash    = new Hashtable();

    public TestSome()
    {
        myHash.Add("key1", new SomeClass());
    }

}

In the webform codebehind: -

 
TestSome myTest = new TestSome();
this.myRepeater.DataSource = myTest.myHash;
this.myRepeater.DataBind();

ASP html
 
<asp:repeater id=myRepeater runat="server">
<ItemTemplate>
    <DIV style="FLOAT: left; MARGIN-LEFT: 10px">
    <asp:Label id="Label2" runat="server" Width="160px">


        <!-- how do i specify that i wish to use the class's properties here? -->
        <%# DataBinder.Eval(Container.DataItem, "myProperty1") %>
       

        <%# DataBinder.Eval(Container.DataItem, "myProperty2") %>

    </asp:Label>
    </DIV>
    </ItemTemplate>
</asp:repeater>

~ Lacuna

Lacuna Aug 15th, 2005 8:42 am
Re: Repeater with Object as Value
 
I found the problem :)

I should be using:

 
<%# ((SomeClass)Container.DataItem).myProperty1 %>

instead of the 'Databinder.Eval' statement. I can then repeat this statement changing the propery value to show each of the properties I want to show.

It works great now.

~ Lacuna


All times are GMT -4. The time now is 4:45 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC