Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting checked value for Eval(bool)

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>
        ...
like image 602
Boris Avatar asked Oct 04 '13 13:10

Boris


2 Answers

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" : "" %> />
like image 54
Nikhil Chavan Avatar answered Nov 07 '22 14:11

Nikhil Chavan


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.

like image 3
Mr47 Avatar answered Nov 07 '22 12:11

Mr47