ASP.NET绑定到CheckBox的经过整场 [英] ASP.NET Binding integer to CheckBox's Checked field

查看:230
本文介绍了ASP.NET绑定到CheckBox的经过整场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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投无效

Exception Details: System.InvalidCastException: Sepcified cast is not valid

即使使用的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()?


【答案】
我通过添加在<一提到一个新的属性,通过延长部分类自动生成的类型href=\"http://stackoverflow.com/questions/1561382/asp-net-binding-integer-to-checkboxs-checked-field/1561480#1561480\">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:


  1. 创建一个自定义的<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx\">TypeConverter这将处理0和1布尔转换

  2. 粘<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverterattribute.aspx\">TypeConverterAttribute在IsUploaded属性指示.NET您的自定义类型转换器。

  1. Create a custom TypeConverter which will handle 0 and 1 to boolean conversions
  2. Stick the TypeConverterAttribute on the IsUploaded property to direct .NET to your custom typeconverter.

这篇关于ASP.NET绑定到CheckBox的经过整场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆