Multimobile Development: Building Applications for any Smartphone
Data-binding array of hashtables
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngdatagridrepeaterdatalist' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Howard Cheng
-- Moved from [aspngcs] to [aspngdatagridrepeaterdatalist] by Douglas Reilly <Click here to reveal e-mail address> --

My Wrox book and the .NET SDK docs don't really seem to cover a
data-binding case like this.

I have a custom table that I want to bind data to. I'm using a Repeater
object because each cell is full of spans and classes, so it looks like:

[asp:Repeater id="myRepeater" runat="server"]
[ItemTemplate]
[tr]
[td class="xxx"][%# Container.DataItem("fraction") %][/td]
[td class="yyy"][%# Container.DataItem("something") %][/td]
(there are about 6 more cells)
[/tr]
[/ItemTemplate]
[/asp:Repeater]

So I was thinking of making an array of hashtables like this. I can't just
bind to the SqlDataReader because there are some conditional values that
vary based on the data.

sql = "SELECT ..."
comm = new SqlCommand(sql,comm);
reader = comm.ExecuteReader();

ArrayList list = new ArrayList();
Hashtable tmp = null;

while (reader.Read())
{
tmp = new Hashtable();
tmp["fraction"] = reader["fraction"].ToString();

tmp["something"] = (reader["something"].ToString() == "blah") ? value1
: value2;

// etc.

list.Add(tmp);
}
reader.Close();

myRepeater.DataSource = list;
myRepeater.DataBind();

But unfortunately this doesn't work. Either that, or I can't figure out the
right expression to use in data-binding. The example that comes with the
Wrox book is <%# Container.DataItem("field") %>, but I get a compilation
error saying it's expecting a property instead of a method. I change it to
index using brackets, and it complains "Cannot apply indexing with [] to an
expression of type 'object'".

So I try <%# DataBinder.Eval(Container.DataItem,"field") %> and it tells me
System.Collections.Hashtable does not have a property called 'field'.

What's the correct way to go about doing this?

Thanks in advance.

::::::::::::::::::::::
Howard Cheng
Click here to reveal e-mail address
AIM: bennyphoebe
ICQ: 47319315

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Scott Mitchell (VIP)
Howard, a great brain stumper! I played around with this a bit and
solved the problem for you. First off, realize that in displaying its
data, the Repeater control loops through the IEnumerable interfaced
object specified in its DataSource property. We can get access to the
current item in the loop inside the <ItemTemplate> tag in the Repeater
by using:

    Container.DataItem

In your case, this returns an instance of the Hashtable class of the
current item in the ArrayList. To access the various fields, you'd
do:

    <%# Container.DataItem.Item("FieldName") %>

Where FieldName is the name of the Hashtable entry, such as "fraction"
and "something" in your code below. I have a simple example I worked
up to figure all this out, and that follows my sig. hth

Scott Mitchell
Click here to reveal e-mail address
http://www.4GuysFromRolla.com/
http://www.ASPMessageboard.com/
http://www.ASPFAQs.com/

* When you think ASP, think 4GuysFromRolla.com!

<%@ Page Language="VB" %>
<script runat="server">

sub Page_Load(sender as Object, e as EventArgs)
Dim z as New ArrayList()
Dim x as Hashtable

x = New HashTable()
x.Add("Name", "Scott")
x.Add("Age", 23)
z.Add(x)

x = New Hashtable()
x.Add("Name", "Bob")
x.Add("Age", 44)
z.Add(x)

Repeater1.DataSource = z
Repeater1.DataBind()

end sub

</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<!-- Insert content here --> 
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<%# Container.DataItem.Item("Name") %><br>
</ItemTemplate>
</asp:Repeater>
</p>
</form>
</body>
</html>

| -----Original Message-----
| From: Howard Cheng [mailto:Click here to reveal e-mail address]
| Sent: Friday, April 19, 2002 3:26 PM
| To: aspngDataGridRepeaterDatalist
| Subject: [aspngdatagridrepeaterdatalist] Data-binding array of
| hashtables
|
|
| -- Moved from [aspngcs] to [aspngdatagridrepeaterdatalist]
| by Douglas Reilly <Click here to reveal e-mail address> --
|
| My Wrox book and the .NET SDK docs don't really seem to cover a
| data-binding case like this.
|
| I have a custom table that I want to bind data to. I'm
| using a Repeater
| object because each cell is full of spans and classes, so
| it looks like:
|
| [asp:Repeater id="myRepeater" runat="server"]
| [ItemTemplate]
| [tr]
| [td class="xxx"][%# Container.DataItem("fraction") %][/td]
| [td class="yyy"][%# Container.DataItem("something") %][/td]
| (there are about 6 more cells)
| [/tr]
| [/ItemTemplate]
| [/asp:Repeater]
|
| So I was thinking of making an array of hashtables like
| this. I can't just
| bind to the SqlDataReader because there are some
| conditional values that
| vary based on the data.
|
| sql = "SELECT ..."
| comm = new SqlCommand(sql,comm);
| reader = comm.ExecuteReader();
|
| ArrayList list = new ArrayList();
| Hashtable tmp = null;
|
| while (reader.Read())
| {
| tmp = new Hashtable();
| tmp["fraction"] = reader["fraction"].ToString();
|
| tmp["something"] = (reader["something"].ToString() ==
| "blah") ? value1
| : value2;
|
| // etc.
|
| list.Add(tmp);
| }
| reader.Close();
|
| myRepeater.DataSource = list;
| myRepeater.DataBind();
|
| But unfortunately this doesn't work. Either that, or I
| can't figure out the
| right expression to use in data-binding. The example that
| comes with the
| Wrox book is <%# Container.DataItem("field") %>, but I get
| a compilation
| error saying it's expecting a property instead of a method.
| I change it to
| index using brackets, and it complains "Cannot apply
| indexing with [] to an
| expression of type 'object'".
|
| So I try <%# DataBinder.Eval(Container.DataItem,"field") %>
| and it tells me
| System.Collections.Hashtable does not have a property
| called 'field'.
|
| What's the correct way to go about doing this?
|
| Thanks in advance.
|
|
| ::::::::::::::::::::::
| Howard Cheng
| Click here to reveal e-mail address
| AIM: bennyphoebe
| ICQ: 47319315
|
| | [aspngdatagridrepeaterdatalist] member
| Click here to reveal e-mail address = YOUR ID
| |
http://www.aspfriends.com/aspfriends/aspngdatagridrepeaterdatalist.asp
= JOIN/QUIT

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Doug J. Nelson
I believe you syntax should be:

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

as opposed to

<%# Container.DataItem.Item["fraction"] %>

hth

Doug Nelson
SynApp north
305 Arch St
Cloquet, MN 55720
(218) 878 - 2015=20
(218) 878 - 2019 fax

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
chinghia@rim-tech.com

Hi Howard,
The Hashtable only has "key" and "value" pair.
You should use DataBinder.Eval(Container.DataItem,"key")
or DataBinder.Eval(Container.DataItem,"value")
hth,
Chi Nghia
======================================
Nghia Cao Chi
Click here to reveal e-mail address
R.I.M Technologies - Vietnam
Tel : (84-8) 8 105 916
http://www.rim-tech.com
======================================

Howard Cheng
<Click here to reveal e-mail address.c To: "aspngDataGridRepeaterDatalist"
om> <Click here to reveal e-mail address>
cc:
04/23/2002 01:55 AM Subject: [aspngdatagridrepeaterdatalist] RE: Data-binding array of
Please respond to hashtables
"aspngDataGridRepeate
rDatalist"

Hi Doug,

I've already tried that. I got the following error:

System.Web.HttpException: DataBinder.Eval: 'System.Collections.Hashtable'
does not contain a property with the name fraction.

At 02:07 PM 4/22/2002 -0500, Doug J. Nelson wrote:
[Original message clipped]

::::::::::::::::::::::
Howard Cheng
Click here to reveal e-mail address
AIM: bennyphoebe
ICQ: 47319315

| [aspngdatagridrepeaterdatalist] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/aspngdatagridrepeaterdatalist.asp =
JOIN/QUIT

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
 
System.Collections.ArrayList
System.Collections.Hashtable
System.Collections.IEnumerable
System.ComponentModel.Container
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlDataReader
System.EventArgs
System.Web.HttpException
System.Web.UI.DataBinder




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734