playing with data within the databound repeater

D

darrel

*sigh*...I've asked this before, but have long forgotten the answer.

In the past, I'd often use repeater controls, bind data to it, and then
reference the data fields from within the repeater:

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

More recently, I've been doing more of the building on the codebehind side,
creating a string builder and building the table from there. When doing
that, I access the data like this:

ds.Tables(0).rows(0)("agentMobilePhone")

Both methods work fine and I see advantages and disadvantages to each.

At the moment, I'm building a table via the former...using a repeater
control. I get frustrated when I want to perform some logic on the data on
the the front-end, though.

I can't simply do this:

if DataBinder.Eval(Container.DataItem, "agentMobilePhone") = something then

as that returns a 'container' not declared error.

I can't access the DS from the front end, either:

if ds.Tables(0).rows(0)("agentMobilePhone")

as ds isn't declared.

So, when I want to do some comparison logic on data via a repeater control,
I need to make a new function on the codebehind page just for this, then
pass the data to the function.

My question is: Is there anyway to access one of the items of data in the
bound repeater from the repeater itself...without having to go back into the
codebehind?

(I know there is a way, I've just forgotten how.)

-Darrel
 
K

Karl Seguin

You can simply hook into the ItemDataBound event and do all your processing
in there. Sure it's still in the codebehind, but it's isolated to a single
function. This is without a doubt the right way to go, you can check out
more from my tutorial on databinding:
http://openmymind.net/databinding/index.html

Karl
 
S

Scott Allen

Whatever is inside the construct will eventually wind up in a
statement like:

System.Convert.ToString(<insert your data binding expr here>)

So any expression that can return an object is fair game.

Or are you trying to reference cells in the Repeater?
 
D

darrel

Or are you trying to reference cells in the Repeater?

Right. Let's say I've bound data to the repeater and I now want to reference
the 'userID' field for that current record FROM the repeater itself (not the
codebehind) and perform some logic with it.

Can that be done?

-Darrel
 
D

darrel

This is without a doubt the right way to go, you can check out


Hmm...well, I made it about half way through, but then I got a bit lost.

Let me use a real world example. Let's say I have this:

<repeater OnItemDataBound="repeater_ItemDataBound">
<%# DataBinder.Eval(Container.DataItem, "name") %>
<%# DataBinder.Eval(Container.DataItem, "address") %>
</repeater>

How do I access the individual items via the codebehind function? I'm stuck
on the pseudo code I have below:

sub repeater(ByVal s As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then

'start pseudo code
if 'name'.tostring = "" then that item = "no name given"
'end pseudo code

End If
end sub
 
K

Karl Seguin

If I understand correctly, the article does cover this. Assuming you are
binding to a dataset/datatable/dataview, you'd do something like:

<itemtemplate>
<asp:literal id="name" runat="server" />
</itemtemplate>

dim nameLiteral as Literal = ctype(e.item.FindControl("name"), Literal)

dim row as DataRowView = ctype(e.Item.DataItem, DataRowView)
dim nameValue as string = cstr(row["name"])
if nameValue.length = 0 then
nameLiteral.Text = "no name given"
else
nameLiteral.Text = nameValue
end if



Keep reading :p you'll get to it :)

karl
 
D

darrel

If I understand correctly, the article does cover this. Assuming you are
binding to a dataset/datatable/dataview, you'd do something like:

I'm binding to a repeater.

Ah...OK, so you are replacing all the refernces to the dataitem directly
with literals.

This makes sense in the realm of separating presentation code from logic
code.

OK, I think I'm close. This is what I have (code trimmed for clarification):

ASPX:

<asp:Repeater OnItemDataBound="repeater_agentlist_ItemDataBound"
id=repeater_agentlist runat="server">
<ItemTemplate>
<asp:Literal id=literal_agentMobilePhone runat="server" />

CODEBEHIND:

sub bindData()
Dim DS As New DataSet
repeater_agentlist.DataSource = DS
repeater_agentlist.DataBind()

sub repeater_agentlist_ItemDataBound(ByVal s As Object, ByVal e As
RepeaterItemEventArgs)
dim agentMobilePhoneLiteral as Literal =
ctype(e.item.FindControl("literal_agentMobilePhone"), Literal)
dim row as DataRowView = ctype(e.Item.DataItem, DataRowView)
dim agentMobilePhone as string = cstr(row("agentMobilePhone"))
if agentMobilePhoneLiteral.tostring.length = 0 then
agentMobilePhoneLiteral.Text = ""
else
agentMobilePhoneLiteral.Text = "THIS WORKS?"
end if
end sub

Doesn't quite work. My guess is that I'm using the wrong syntax to grab data
from the dataset. Row is a part of a dataTable, correct?

Let me fiddle with this a bit more...

-Darrel
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top