views:

165

answers:

2

Hi,
I have a Repeater control who has its dataSource setted with a typed object list and in the inline code I want to access to its elements properties inside the ItemTemplate tag. I tried this with the eval expression but it does not work:

<ItemTemplate>
  <tr>
    <td><%# Eval("code") %></td>
    <td><%# Eval("description") %></td>
  </tr>
</ItemTemplate>

Any ideas?
Thank you!!

+1  A: 

You can use: <%# DataBinder.Eval(Container.DataItem, "field name") %>

fbinder
A: 

Does your object have a property called "code". Remember it's case sensitive.

e.g. If your object were...

public class MyObj
{
    public string Code { get; set; }
    public string Description { get; set; }
}

And you were binding a Collection<MyObj> to your datasource,

Then you're repeater would look like...

<asp:repeater id="Repeater1" runat="server">
    <headertemplate>
      <table border="1">
        <tr>
          <td><b>Code</b></td>
          <td><b>Description</b></td>
        </tr>
    </headertemplate>

    <itemtemplate>
      <tr>
        <td> <%# Eval("Code") %> </td>
        <td> <%# Eval("Description") %> </td>
      </tr>
    </itemtemplate>

    <footertemplate>
      </table>
    </footertemplate>
  </asp:repeater>
Eoin Campbell
Thanks Eoin but it's what I tried, it gave me an error about it can only be used in the context of a databound control.
Alfred