Repeater: Can't Refer to Data In A Label

A

Alex Davidson

I have been banging my head against a wall trying to get a server-control to
reference the text of a hidden <asp:label> so for testing I stripped things
down to a bare minimum to try response.write'ing the data to the screen to
see if anything can be seen at all...

I have the following HTML:

<asp:Repeater ID="myRepeater" Runat="server"
OnItemCreated="myRepeater_ItemCreated">
....
<ItemTemplate>
<tr valign="top" ID="BodyRow" runat="server">
<td>
<asp:Label ID="lblImageLabel" Visible="true" Runat=server text=<%#
container.dataitem("ItemETImageFileName") %>><%#
container.dataitem("ItemETImageFileName") %></asp:Label>
</td>
....
</ItemTemplate>

coupled with the following Code-Behind:

Sub myRepeater_ItemCreated(ByVal sender As Object, ByVal e As
RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Header Then
....
ElseIf e.Item.ItemType = ListItemType.Footer Then
....
Else
Dim varImageName As Label
varImageName = CType(e.Item.FindControl("lblImageLabel"), Label)
Response.Write(varImageName.Text & "<BR>")
End if
....

but the response.writes show nothing - can anyone suggest a reason and
solution?

Thanks.
Alex
 
E

Eliyahu Goldin

Alex,

My guess is that the problem is with the label id. A control Id should be
unique across the page. If you have several items in the repeater, and you
specify id in the template, every new item will have automatically assigned
ids for the <tr> and <asp:label>. So varImageName.Text should always return
the same value of the only label with this id. Apparently, it is empty.

Interested to know if that is right.

Eliyahu
 
A

Alex Davidson

Good logic but I don't think that's what happening....

When I look at the page's source, they are rendered as:
<span id="myRepeater__ctl1_lblImageLabel">NONE</span>
<span id="myRepeater__ctl2_lblImageLabel">ET988486.gif</span>
etc.

The logic I am using to get to the data is the same I use for DataGrids but
something's not quite right.
 
A

Alex Davidson

They are all unique if you look at the code. Anyway, I got it working by
changing one thing:
OnItemCreated="myRepeater_ItemCreated"
to
OnItemDataBound="myRepeater_ItemCreated"

My logic holds, it's just that it was trying to do it before the data was
bound to the controls
 
E

Eliyahu Goldin

It looks to me as confirmation of my logic. You should use UniqueID property
of the label as search parameter in FindControl.

Eliyahu
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top