Bor*_*ris 8 c# asp.net checkbox
我有一个房产
public bool AutoRenew
{
    get;
    set;
}
在页面中:
<input type="checkbox" checked='<%# Eval("AutoRenew") %>' />
但总是检查,即使属性的值是false.
我尝试了以下变化:
<input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) %>' />
<input type="checkbox" checked='<%# Convert.ToBoolean(Eval("AutoRenew")) == true %>' />
<input type="checkbox" checked='<%# (Boolean)Eval("AutoRenew") %>' />
但没有任何作用,它一直在检查.表达式应该是什么样的?
编辑:这是页面中有问题的部分:
...
<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>
        ...
Nik*_*van 21
您正在使用普通的html复选框
要将数据绑定到palin html复选框,您必须使用checked ="checked"
如果您使用ASP.NET Checkbox控件,那么您的原始代码将顺利运行.
绑定数据时,普通html控件和ASP.NET控件之间存在差异.
 //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" : "" %> />