Binding the Checked Property of a CheckBox within a TemplateItem

Frinavale picture Frinavale · Jan 20, 2010 · Viewed 39.1k times · Source

For the life of me I cannot bind the Checked property of a CheckBox control within a TemplateField (declaritively).

I have tried:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="deactivated" runat="server" checked="<%#Eval("Deactivated")%>"></asp:CheckBox>
    </ItemTemplate>
<asp:TemplateField>

and

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="deactivated" runat="server" checked="<%#Eval(Container.DataItem, "Deactivated")%>"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateField>      
</asp:TemplateField>

I keep seeing a warning stating:

Cannot create an object of type 'System.Boolean' from it's string representation' 'for the 'Checked' property

What am I doing wrong?

Answer

keyboardP picture keyboardP · Jan 20, 2010

It may be because of the double quotes you've used. Try:

checked= '<%# Eval("Deactivated") %>'