views:

81

answers:

4

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?

Thanks for your help,

-Frinny

A: 

Eval is for evaluating expressions.

Try Bind.

checked='<%#Bind("Deactivated")%>'
citronas
A: 

its best to handle this via code behind in the control's rowdatabound event (assuming its 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).

deostroll
+1  A: 

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

checked= '<%# Eval("Deactivated") %>'
keyboardP
You are right. I won't work with the double quotes as outer quotes
citronas
+1  A: 

Use single quotes around the property value:

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

Ryan