DannyT Posted August 14, 2005 Posted August 14, 2005 I have a hashtable that i have populated with instances of a custom class, the class has properties that i wish to access when iterating through the hashtable using a repeater. haven't got access to the code right now (at office), but if anyone can tell me how to return the values of a class' properties when iterating through a hashtable using a repeater I would be xtremely dot netting grateful. If needs be i'll post the code when i'm in first thing tomorrow. The error i'm getting is somethign along the lines of myClass.myHash does not contain the property myProperty, when it blatently does. Cheers! Quote
Administrators PlausiblyDamp Posted August 14, 2005 Administrators Posted August 14, 2005 Hashtables store objects and return objects. The result is you can store anything in it without regard to data type but it always returns an object. You will need to cast this returned object to the correct type to use it's properties. i.e. Dim h As New Hashtable h.Add("test", New Button) DirectCast(h("test"), Button).Text = "Test thing" System.Collections.Hashtable h = new Hashtable(); h.Add("Test", new Button()); ((Button) h["Test"]).Text="Test Thing"; Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
DannyT Posted August 15, 2005 Author Posted August 15, 2005 (edited) Thanks plausibly, sorry i'm still not quite with it, how do i refer to these properties when using the repeater? SomeClass myClass = new SomeClass(); //Some Class contains the property "myPropert" this.myHash.Add("key1", myClass); this.myRepeater.DataSource = this.myHash; this.myRepeater.DataBind(); <asp:repeater id=rptFeaturedUserBuddies 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 myProperty here? --> <%# DataBinder.Eval(Container.DataItem, "myProperty") %> </asp:Label> </DIV> </ItemTemplate> </asp:repeater> Edited August 15, 2005 by DannyT Quote
DannyT Posted August 15, 2005 Author Posted August 15, 2005 Use repeater to iterate hashtable containing objects with properties Here is a better description/example of what i'm trying to achieve: - 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: - [csharp] 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()); } } [/csharp] In the webform codebehind: - [csharp] TestSome myTest = new TestSome(); this.myRepeater.DataSource = myTest.myHash; this.myRepeater.DataBind(); [/csharp] 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") %> <br> <%# DataBinder.Eval(Container.DataItem, "myProperty2") %> </asp:Label> </DIV> </ItemTemplate> </asp:repeater> Quote
DannyT Posted August 15, 2005 Author Posted August 15, 2005 Thanks plausibly, you had given me the answer i was just dumb on syntax, figured it out now: - <%# ((SomeClass)Container.DataItem).myProperty1 %> Quote
DannyT Posted August 15, 2005 Author Posted August 15, 2005 I just discovered that had I not been lazy in the first instance and used proper getters and setters instead of public properties then the DataBinder.Eval would have worked! Mildly annoying solution that took me best part of 2 days to figure out! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.