467,211 Members | 1,206 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,211 developers. It's quick & easy.

playing with data within the databound repeater

*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
Nov 18 '05 #1
  • viewed: 2208
Share:
8 Replies
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:e6*************@TK2MSFTNGP11.phx.gbl...
*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

Nov 18 '05 #2
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 30 Nov 2004 11:47:17 -0600, "darrel" <no*****@hotmail.com>
wrote:
*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


Nov 18 '05 #3
> 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


Thanks, Karl...reading it now...

-Darrel
Nov 18 '05 #4
> 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
Nov 18 '05 #5
Ah, yes. Karl has a good suggestion for that approach.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 30 Nov 2004 13:26:12 -0600, "darrel" <no*****@hotmail.com>
wrote:
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


Nov 18 '05 #6
> 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

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
Nov 18 '05 #7
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
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

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

Nov 18 '05 #8
> 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
Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by mark | last post: by
4 posts views Thread by Roshawn | last post: by
1 post views Thread by Majek | last post: by
4 posts views Thread by Rudy | last post: by
4 posts views Thread by Joel | last post: by
4 posts views Thread by jjack100@gmail.com | last post: by
6 posts views Thread by Guabble | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.