help with repeater, how to code it in code-behind page

T

Tee

Hi,

I have a Repeater with a DataReader.
I can successfully let the code run with something like this
<asp:label id="testlabel" text='<%# DataBinder.Eval(Container.DataItem,
"Name") %>' runat="server"></asp:label>

but now I would like to code this few line ItemTemplate in my code-behind
page, how do I do it ?


Thanks,
Tee
 
R

Raterus

I tried to do the same thing you are doing when I first was learning asp.net I hated the idea of using <%# %> Let me just say, that using <%# %> in this case is MUCH Easier, than doing it from codebehind!

But to do it in codebehind, in the ItemDataBound event you need something like this (This is an example taken from a datagrid, a repeater should be just about the same)

If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim lbl As Label = DirectCast(e.Item.Controls(0).Controls(0), Label)
lbl.text = e.Item.DataItem("Name")
End If

The tricky part which makes this more difficult is finding the right nested sequence of controls that corresponds to the label. And what makes it even more difficult, if you ever change the structure around, you'll need to change this code around.

Hope this helps
--Michael
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top