Results 1 to 2 of 2

Thread: Repeater with Object as Value

  1. #1

    Thread Starter
    New Member Lacuna's Avatar
    Join Date
    May 2005
    Posts
    6

    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: -

    Code:
     
    
    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: -

    Code:
     
    TestSome myTest = new TestSome(); 
    this.myRepeater.DataSource = myTest.myHash; 
    this.myRepeater.DataBind();
    ASP html
    Code:
     
    <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

  2. #2

    Thread Starter
    New Member Lacuna's Avatar
    Join Date
    May 2005
    Posts
    6

    Re: Repeater with Object as Value

    I found the problem

    I should be using:

    Code:
     
    <%# ((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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width