ASP.NET Binding integer to CheckBox's Checked field

Posted by Sung Meister on Stack Overflow See other posts from Stack Overflow or by Sung Meister
Published on 2009-10-13T16:27:48Z Indexed on 2010/05/21 16:10 UTC
Read the original article Hit count: 382

Filed under:
|
|
|
|

I have a following ListView item template, in which I am trying to bind integer value to Checked property of CheckBox.

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>

But ASP.NET complains that

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

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"))) %>' />

How can I convert 0's and 1's to boolean using Bind()?


[ANSWER] I have extended auto-generated type through partial class by adding a new property mentioned in the answer by Justin

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about databinding