Tutorial :How to access to the elements properties inside a Repeater?



Question:

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!!


Solution:1

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


Solution:2

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>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »