C# 为 Eval(bool) 设置选中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19182859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting checked value for Eval(bool)
提问by Boris
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
.
但它总是被检查,即使属性的值为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>
...
采纳答案by Nikhil Chavan
You are using plain html checkbox
您正在使用纯 html 复选框
to bind data to palin html checkbox you must use checked="checked"
要将数据绑定到 palin html 复选框,您必须使用 checked="checked"
If you use ASP.NET Checkbox control then your original code will work smoothly.
如果您使用 ASP.NET Checkbox 控件,那么您的原始代码将能够顺利运行。
There is difference between plain html controls & ASP.NET controls when binding data.
绑定数据时,纯 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" : "" %> />
回答by Mr47
Desired output HTML should get you on the way:
所需的输出 HTML 应该可以帮助您:
<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.
这意味着,要不选中复选框,您根本不应checked
在输出中提及该属性,即使值为 false 也不应。
回答by Satpal
Add checked
attribute if Convert.ToBoolean(Eval("AutoRenew"))
is true
checked
如果Convert.ToBoolean(Eval("AutoRenew"))
是,则添加属性true
<input type="checkbox"
<%# Convert.ToBoolean(Eval("AutoRenew")) ? "checked" : string.Empty %> />
回答by reza akhlaghi
you can check anytype value in Grid_RowDataBound Event :
您可以在 Grid_RowDataBound Event 中检查 anytype 值:
aspx :
ASP :
<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:
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;
}
}
回答by Dipendu Paul
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 :
我面临着接受答案的问题。如果有人正在寻找让 asp 复选框选中的属性起作用,这里是对我有用的代码:
<td><asp:CheckBox ID="chkHasAbility" runat="server" Checked='<%#bool.Parse(Eval("HasAbility").ToString())%>' /> </td>