简体   繁体   中英

ASP.NET Repeater - Eval() for bool?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM