Repeater with Object as Value

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: May 2005
Posts: 19
Reputation: Lacuna is an unknown quantity at this point 
Solved Threads: 0
Lacuna's Avatar
Lacuna Lacuna is offline Offline
Newbie Poster

Repeater with Object as Value

 
0
  #1
Aug 15th, 2005
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: -

  1.  
  2.  
  3. Class SomeClass
  4. {
  5. public String myProperty1
  6. public String myProperty2
  7.  
  8. public SomeClass()
  9. {
  10. this.myProperty1 = "hello";
  11. this.myProperty2 = "goodbye";
  12. }
  13. }
  14.  
  15.  
  16. Class TestSome
  17. {
  18. public Hashtable myHash = new Hashtable();
  19.  
  20. public TestSome()
  21. {
  22. myHash.Add("key1", new SomeClass());
  23. }
  24.  
  25. }

In the webform codebehind: -

  1.  
  2. TestSome myTest = new TestSome();
  3. this.myRepeater.DataSource = myTest.myHash;
  4. this.myRepeater.DataBind();

ASP html
  1.  
  2. <asp:repeater id=myRepeater runat="server">
  3. <ItemTemplate>
  4. <DIV style="FLOAT: left; MARGIN-LEFT: 10px">
  5. <asp:Label id="Label2" runat="server" Width="160px">
  6.  
  7.  
  8. <!-- how do i specify that i wish to use the class's properties here? -->
  9. <%# DataBinder.Eval(Container.DataItem, "myProperty1") %>
  10.  
  11.  
  12. <%# DataBinder.Eval(Container.DataItem, "myProperty2") %>
  13.  
  14. </asp:Label>
  15. </DIV>
  16. </ItemTemplate>
  17. </asp:repeater>
  18.  

~ Lacuna
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 19
Reputation: Lacuna is an unknown quantity at this point 
Solved Threads: 0
Lacuna's Avatar
Lacuna Lacuna is offline Offline
Newbie Poster

Re: Repeater with Object as Value

 
0
  #2
Aug 15th, 2005
I found the problem

I should be using:

  1.  
  2. <%# ((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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC