ASP.NET Binding integer to CheckBox's Checked field

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

13.10.2009 16:27:48
Is making "IsUploaded" a bit or other boolean datatype instead of an int an option?
kscott 13.10.2009 16:32:23
@kscott: "IsUploaded" is a bit.
dance2die 13.10.2009 16:59:05
What kind of datasource are you binding to? Binding a checkbox to a bit using an objectdatasource pointing to a table adapter works with no errors for me.
kscott 13.10.2009 17:10:02
@kscott: It is an auto-generated type from Entity framework.
dance2die 13.10.2009 17:17:07
I don't know why I cannot Upvote. Anyways, sorry about not being able to vote all great answers here. It says that I cannot upvote unless the answers are edited??? ...
dance2die 13.10.2009 17:19:24
6 ОТВЕТОВ
РЕШЕНИЕ

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. 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.
7
13.10.2009 17:28:56

How about adding a property to your class which does the conversion?

public bool IsUploadedBool
{
  get { return IsUploaded == 1; }
}

and then bind to this IsUploadedBool property instead of directly to the underlying INT.

Marc

3
13.10.2009 17:00:50
@marc_s: You meant double "==" not "=" :)
dance2die 13.10.2009 17:00:10

Kind of a cheesy work around would be to use a drop down list with list items to give the same effect:

<asp:DropDownList ID="ddlBool" runat="server" SelectedValue= '<%# Bind("IsUploaded") %>'>
    <asp:ListItem Text="True" Value="1" />
    <asp:ListItem Text="False" Value="0" />
</asp:DropDownList>
2
13.10.2009 16:44:04
Sorry scott but I have to stick to "CheckBox" for my particular case. But thank you for giving me an insight on this.
dance2die 13.10.2009 17:01:06
@kscott: Would you be able to make change to your answer? (add a dot or etc.) so that I can upvote your answer? I consider your answer to be a valid option for my case as well. Thanks.
dance2die 13.10.2009 18:50:56
2
3.05.2010 17:30:14

How about (btw i am using a stored procedure)

Aspx page

<asp:CheckBox ID="social_facebook" runat="server"  Checked='<%# Bind("True") %>' />Facebook

Code behind

cmd.Parameters.Add("@p_facebook", SqlDbType.Bit).Value = social_facebook.Checked;
1
21.05.2010 16:08:56

Solution:

Hi, I'm also using Ajax rating inside a GridView and I ran into this problem. After looking through several forums, I tried this and it worked for me. I hope it helps you:

<%# Convert.ToInt32(Eval("EVALUATION")) %>

returns an integer, since you're using Checked which is an integer.

0
21.08.2013 01:36:58