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