Pergunta

I have a simple repeater, where I want to display checked/unchecked checkboxes:

<asp:Repeater ID="myRepeater" runat="server">
   <ItemTemplate>
      <td>
         <asp:CheckBox runat="server" ID="cb" Checked='<%# Eval("value") %>' />
      </td>
    </ItemTemplate>
 </asp:Repeater>


var list = new List<bool>();

list.Add(true);
list.Add(false);

myRepeater.DataSource = list;
myRepeater.DataBind();

but I get an error:

DataBinding: 'System.Boolean' does not contain a property with the name 'value'.

How to fix it ?

Foi útil?

Solução

Try this:

<asp:Repeater ID="myRepeater" runat="server">
   <ItemTemplate>
      <td>
         <asp:CheckBox runat="server" ID="cb" Checked='<%# Container.DataItem %>' />
      </td>
    </ItemTemplate>
 </asp:Repeater>

Eval(x) is a shortcut for Databinder.Eval(Container.DataItem, x). Which evalutes the property/etc x for the row item in the repeater. But you don't need to evaluate anything, you just want the raw DataItem.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top