How do I access the current record in an asp:Repeater?
Suppose I have an asp:Repeater that I am using to display a list of names and phone numbers.
<asp:Repeater runat="server">
<ItemTemplate>
<p><%# Eval("name") %></p>
<p><%# Eval("phoneNumber") %></p>
</ItemTemplate>
</asp:Repeater>
Now, let's make it开发者_StackOverflow more fun. Let's suppose that there is one or more phone number per name. I feel like I could use a repeater within a repeater to repeat the phone numbers. I could also use a for
loop in my asp code. However, the Eval
function is the only way I know of to access the current record at runtime and Eval only returns a string and only runs in <%# Eval("X") %>. How can I get the list of phone numbers that is a member of the current record?
First, change
<%# Eval("name") %>
to
<asp:Label runat="server" id="lblName" Text='<%# Eval("name") %>'></asp:Label>
Then in the code-behind you can use FindControl()
foreach (RepeaterItem currentItem in Repeater1.Items)
{
Label l = (Label)currentItem.FindControl("lblName");
// Now you can access it as you would any other label.
Response.Write(l.Text);
}
精彩评论