问题描述
我有以下ListView项模板,其中我试图绑定整数值CheckBox的经过
属性。
I have a following ListView item template, in which I am trying to bind integer value to Checked
property of CheckBox.
IsUploaded
值只包含0和1 ...
IsUploaded
value contains only 0 and 1...
<asp:ListView ID="trustListView" runat="server">
<ItemTemplate>
<asp:CheckBox ID="isUploadedCheckBox" runat="server"
Checked='<%# Bind("IsUploaded") %>' />
</ItemTemplate>
</asp:ListView>
但ASP.NET抱怨
But ASP.NET complains that
异常详细信息:System.InvalidCastException:Sepcified投无效
即使使用的DataBinder.Eval()
以下工作code,
我需要有一个双向绑定,因此需要使用绑定()
。
Even though following code using DataBinder.Eval()
works,
I need to have a 2-way binding, thus need to use Bind()
.
<asp:CheckBox ID="isUploadedCheckBox2" runat="server"
Checked='<%# Convert.ToBoolean(
DataBinder.Eval(Container.DataItem, "IsUploaded"))) %>' />
我如何转换0和1的使用布尔值绑定()
How can I convert 0's and 1's to boolean using Bind()
?
【答案】
我通过添加在answer由Justin
推荐答案
如果你愿意改变类,上这是一个布尔值类添加属性
If you're willing to change the class, add a property on the class that's a boolean
public bool IsUploadedBoolean
{
get { return IsUploaded != 0; }
set { IsUploaded = value ? 1 : 0; }
}
如果没有,你可能有一个类型转换器的成功:
If not, you may have success with a TypeConverter:
- 创建一个自定义的TypeConverter这将处理0和1布尔转换
- 粘TypeConverterAttribute在IsUploaded属性指示.NET您的自定义类型转换器。
- Create a custom TypeConverter which will handle 0 and 1 to boolean conversions
- Stick the TypeConverterAttribute on the IsUploaded property to direct .NET to your custom typeconverter.
这篇关于ASP.NET绑定到CheckBox的经过整场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!