asp:repeater and hashtable - also in languages.csharp

J

Joe Fawcett

Sorry about the multi post, I thought I'd sent to both groups simultaneously but
somehow it failed to find this one the first time.

I'm having a problem binding an asp:repeater control to a Hashtable. Originally
my code was:

<asp:Repeater id="rptFamily" runat="server" DataSource="<%# family %>">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Key") %> =
<%# DataBinder.Eval(Container.DataItem, "Value") %>
</ItemTemplate>
</asp:Repeater>

which was bound to a Hashtable and all works fine. I then started reading that
Databind.Eval was an overkill, I could just use:

<asp:Repeater id="rptFamily" runat="server" DataSource="<%# family %>">
<ItemTemplate>
<%# Container.DataItem.Key %> =
<%# Container.DataItem.Value %>
</ItemTemplate>
</asp:Repeater>

but although I can build the site when accessing the page I get an error that :

'object' does not contain a definition for 'Key'

pointing to the first changed line. I've found lots of examples on the Web
showing this second version, are they all perhaps from a beta version or is it
something else?

Thanks
 
K

Karl Seguin

Joe:

Try:

<%# ((DictionaryEntry)Container.DataItem).Key %>
<%# ((DictionaryEntry)Container.DataItem).Value%>

Container.DataItem is of type object
object doesn't container a "key" or "value" property

You need to explicitely cast it to the underlying type that is being bound.
When you are binding to a DataSet/DataTable/DataView, Container.DataItem is
always a "DataRowView"...

DataBinder.Eval does have performance implications, but on the flip side it
lets you change the underlying datasource implementation without breaking
your code...ie, using DataBinder.Eval you could switch to a dataset, and as
long as it had a property named "key" and "value" everything would continue
to work...

Karl
 
J

Joe Fawcett

Karl Seguin said:
Joe:

Try:

<%# ((DictionaryEntry)Container.DataItem).Key %>
<%# ((DictionaryEntry)Container.DataItem).Value%>

Container.DataItem is of type object
object doesn't container a "key" or "value" property

You need to explicitely cast it to the underlying type that is being bound.
When you are binding to a DataSet/DataTable/DataView, Container.DataItem is
always a "DataRowView"...

DataBinder.Eval does have performance implications, but on the flip side it
lets you change the underlying datasource implementation without breaking
your code...ie, using DataBinder.Eval you could switch to a dataset, and as
long as it had a property named "key" and "value" everything would continue
to work...

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Thanks, it works. So does that mean that the example code I've seen, using a
HashTable, was wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top