Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding the Checked Property of a CheckBox within a TemplateItem

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?

like image 516
Frinavale Avatar asked Jan 20 '10 18:01

Frinavale


3 Answers

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

checked= '<%# Eval("Deactivated") %>'
like image 176
keyboardP Avatar answered Nov 08 '22 20:11

keyboardP


Use single quotes around the property value:

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

like image 3
Ryan Avatar answered Nov 08 '22 21:11

Ryan


It's best to handle this via code-behind in the control's rowdatabound event (assuming it's a gridview).

if (e.Row.RowType == RowType.DataRow)
{
    CheckBox chk = (CheckBox) GridView1.FindControl("deactivated");
    chk.Checked = true;
}

Note: The abv code may contain errors ...

OR,

Retrieve the data in such a manner that that particular field the checkbox is trying to bind to should be a field of type bit (i.e. it can either have 1 or 0).

like image 1
deostroll Avatar answered Nov 08 '22 21:11

deostroll