ASP.NET - use asp: CheckBox on ListView

I am using a .NET 3.5 ListView control and would like to display the value of a TINYINT field as a checkbox (0 = False, 1 = True).

How to do it?

I have tried:

<asp:CheckBox ID="freight_foundCheckbox" runat="server" 
Checked='<%# Eval("found") %>'  />

      

But this leads to Cast error.

+2


source to share


3 answers


Try the following:



<asp:CheckBox ID="freight_foundCheckbox" runat="server" Checked='<%# Convert.ToBoolean(Eval("found")) %>'  />

      

+3


source


This won't work on an int field. You have to make sure the property you are binding to is a boolean / bool.



+3


source


The above example throws an exception. You need to convert the value to bool:

<asp:CheckBox ID="freight_foundCheckbox" runat="server" Checked='<%# (int)Eval("found") == 1 ? true : false  %>' />

      

+1


source







All Articles