Reputation: 10266
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>
...
Upvotes: 10
Views: 30304
Reputation: 1715
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" : "" %> />
Upvotes: 24
Reputation: 2753
I was facing issue with accepted answer. In case somebody is looking for getting asp checkbox checked property to work, here is code which worked for me :
<td><asp:CheckBox ID="chkHasAbility" runat="server" Checked='<%#bool.Parse(Eval("HasAbility").ToString())%>' /> </td>
Upvotes: 1
Reputation: 760
you can check anytype value in Grid_RowDataBound Event :
aspx :
<asp:GridView ID="GridMain" runat="server" OnRowDataBound="GridMain_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="grid_chkbox" Enabled="false" />
</ItemTemplate
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs:
protected void GridMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the checkboxes in the template field.
CheckBox grid_chkbox= (CheckBox)e.Row.FindControl("grid_chkbox");
//find boolean value in current record
grid_chkbox.Checked = e.Row.DataItem.boolvalue;
}
}
Upvotes: 1
Reputation: 133423
Add checked
attribute if Convert.ToBoolean(Eval("AutoRenew"))
is true
<input type="checkbox"
<%# Convert.ToBoolean(Eval("AutoRenew")) ? "checked" : string.Empty %> />
Upvotes: 2
Reputation: 2655
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.
Upvotes: 3