I have a property
public bool AutoRenew
{
    get;
    set;
}
And in the page:
<input type="checkbox" checked='<%# Eval("AutoRenew") %>' />
but it is always checked, even if the value of the property is false.
I tried the following variations:
<input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) %>' />
<input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) == true %>' />
<input type="checkbox" checked='<%# (Boolean)Eval("AutoRenew") %>' />
but nothing works, it keeps being checked. What should the expression look like?
EDIT: Here is the problematic part in the page:
...
<asp:ListView ID="MyListView" runat="server">
    <LayoutTemplate>
        <table class="ms-listviewtable" style="background-color: White;">
            <tr class="ms-viewheadertr ms-vhltr">
                <th class="ms-vh-icon" scope="col">
                    <input type="checkbox" />
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Training Item</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Training Task Type</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Due Date</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Auto-Renew</a></div>
                </th>
                <th class="ms-vh2">
                    <div class="ms-vh-div"><a>Training Reason</a></div>
                </th>
            </tr>
            <tr id="itemplaceholder" runat="server"></tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr class="ms-itmhover">
            <td class="ms-vb-itmcbx ms-vb-firstCell">
                <input type="checkbox" class="s4-itm-cbx" />
            </td>
            <td class="ms-vb-title">
                <div class="ms-vb itx"><a><%# Eval("Title")%></a></div>
            </td>
                <td class="ms-vb2">
                    <asp:DropDownList ID="TaskTypeDropDownList" runat="server">
                    </asp:DropDownList>
                </td>
                <td class="ms-vb2"><%# Eval("DueDate")%></td>
                <td class="ms-vb2" style="text-align: center;">
                    <input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) %>' />
                </td>
                <td class="ms-vb2"><%# Eval("TrainingReason")%></td>
            </tr>
        </ItemTemplate>
        ...
                You are using plain HTML checkbox
to bind data to plain HTML checkbox you must use checked="checked"
If you use ASP.NET Checkbox control then your original code will work smoothly.
There is a difference between plain HTML controls & ASP.NET controls when binding data.
 //for asp.net checkbox
 <asp:CheckBox  ID="IdCheckBox" runat="server" Checked="<%# Convert.ToBoolean(Eval("AutoRenew")) %>"  />
//for plain html checkbox
<input type="checkbox" <%# Convert.ToBoolean(Eval("AutoRenew")) ? "checked" : "" %> />
                        Desired output HTML should get you on the way:
<input type="checkbox" checked="checked" />
<input type="checkbox" />
This means that, to NOT check the checkbox, you should not mention the checked attribute in the output at all, not even with a value of false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With