How do I bind an array to a repeater?

Z

Zenobia

Hello folks,

How do I bind an array to a repeater?

or bind an array to a hyperlink (which is repeated inside a
repeater) ?

I get this message:

Compiler Error Message: BC30456: 'DataItem' is
not a member of 'System.Web.UI.Control'.

for line below that begins with <li>

This is my simplified example code. I have included 2 examples
both of which fail.

+++ +++ +++ +++
<Script Runat="Server">

Sub Page_Load()
Dim aryDates() As String = {"2004-07-04", "2004-07-05",
"2004-07-06"}

rptRHS_Menu.DataSource = aryDates
rptRHS_Menu.DataBind()

End Sub

</Script>

<html>
<body>
<asp:Repeater
ID="rptRHS_Menu"
Runat="Server" />
<ItemTemplate>
<li><a href="<%# String.Concat( Container.DataItem,
".html") %>"><%# Container.DataItem %></a></li>
</ItemTemplate>
</asp:Repeater>
</body>
</html>
+++ +++ +++ +++

<Script Runat="Server">
Sub Page_Load()
Dim aryDates As ArrayList
aryDates = New ArrayList

aryDates.Add ("2004-07-04")
aryDates.Add ("2004-07-05")
aryDates.Add ("2004-07-06")

aryDates.Reverse

rptRHS_Menu2.DataSource = aryDates
rptRHS_Menu2.DataBind()
End Sub
</Script>
<html>
<body>
<form Runat="Server">
<asp:Repeater
ID="rptRHS_Menu2"
Runat="Server" />
<ItemTemplate>
<li>
<a href='<%# String.Format( "{0}.html",
Container.DataItem ) %>'><%# String.Format( "{0}",
Container.DataItem) %></a>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
 
Z

Zenobia

Oh my God. I've been told I can't do it. I have to put the data
into a hash table and add that to the array. Like this:

Surely there is a simpler way. Why can't I just refer to the
bound array as Container.DataItem(0) ?

I'm sure I've seen examples like that.

Here is the solution that's been indicated to me. There must be
a simpler way than this:

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

Function Add2Hash(dtd As String) As HashTable
Dim hshDates As HashTable
hshDates = New HashTable()
hshDates.Add("Date", dtd)
Return hshDates
End Function

Sub Page_Load(sender as Object, e as EventArgs)

Dim aryDates As ArrayList
aryDates = New ArrayList

aryDates.Add(Add2Hash("2004-07-04"))
aryDates.Add(Add2Hash("2004-07-05"))
aryDates.Add(Add2Hash("2004-07-06"))

aryDates.Reverse

rptRHS_Menu3.DataSource = aryDates
rptRHS_Menu3.DataBind()

End Sub

</script>

<html>
<head>
</head>
<body>
<form Runat="Server">
<ul>
<asp:Repeater id="rptRHS_Menu3" Runat="Server">
<ItemTemplate>
<li><a href='<%# String.Format("{0}.html",
Container.DataItem.Item("Date")) %>'><%#
Container.DataItem.Item("Date") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
</body>
</html>
 
S

Scott Allen

Zenobia:

The code you posted in your original example works fine for me.
Perhaps you could post the complete code as the error must be
somewhere else.

I assure you it is quite possible to bind to an array or an ArrayList.
 
Z

Zenobia

Zenobia:

The code you posted in your original example works fine for me.
Perhaps you could post the complete code as the error must be
somewhere else.

I assure you it is quite possible to bind to an array or an ArrayList.

Thanks, but I think you must've fixed my code error when you got
it to work.

The error was due to an improperly closed <asp:repeater> tag.

I'm closing it here:

<asp:Repeater
ID="rptRHS_Menu"
Runat="Server" />

and including a </asp:Repeater> below that! The example should
be changed to

<asp:Repeater
ID="rptRHS_Menu"
Runat="Server">

for it to work.

The other 'solution' I posted in reply to this post was over
complex. I don't need the hash table.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top