I am using ASP.NET 4.5 Model Binding to present items in a ListView control with editing.
<asp:ListView ID="Results" runat="server" SelectMethod="SelectClientStatus" DataKeyNames="ID" ItemPlaceholderID="itemPlaceHolder" ItemType="ClientStatus" OnItemCommand="Results_ItemCommand" InsertItemPosition="LastItem" UpdateMethod="UpdateClientStatus" InsertMethod="InsertClientStatus">
<LayoutTemplate>
<table>
<tr>
<th runat="server">
<asp:LinkButton ID="SortByDescription" runat="server" ClientIDMode="Static" CommandName="Sort" CommandArgument="Description" Text="Description" />
</th>
<th>Active</th>
<th></th>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</table>
<agp:PagerControl runat="server" ID="PagerControl" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%#: Item.Description%>
</td>
<td>
<%#: Item.IsClientActive %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" ClientIDMode="Static" CommandName="Edit" CommandArgument="<%#: Item.ID %>" Text="Edit" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
When I add my EditItemTemplate, I have a Checkbox and I am trying to bind the Checked property to the model…
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Description" runat="server" Text="<%#: BindItem.Description%>" />
</td>
<td>
<asp:CheckBox ID="IsActive" runat="server" Checked="<%#: BindItem.IsClientActive %>" />
</td>
<td>
<asp:LinkButton ID="Update" runat="server" ClientIDMode="Static"
CommandName="Update" CommandArgument="<%#: Item.ID %>"
Text="Update" />
<asp:LinkButton ID="Cancel" runat="server" ClientIDMode="Static"
CommandName="Cancel" CommandArgument="<%#: Item.ID %>"
Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
This is where the problem starts, running the page now shows a message of “CS0030: Cannot convert type ‘string’ to ‘bool'”, prompting with the line…
<td>
<asp:CheckBox ID="IsActive" runat="server" Checked="<%#: BindItem.IsClientActive %>" />
</td>
What have I missed? How do I bind the value of IsClientActive to the Checked property of the Checkbox control? It is worth noting that, within the model, the IsClientActive property is defined as a Boolean and not nullable.
My bad;
Checked="<%#: BindItem.IsClientActive %>"should have beenChecked="<%# BindItem.IsClientActive %>"(note the omission of the colon (:))