How do I do data binding with a checkbox?

A

Alan Silver

Hello,

I have a database table with a char(1) filed to indicate whether or not
an event is shown. If the value is 'y' it is, and if it's 'n' it's not.

I am trying to use this value in a checkbox (in a DataList), so that
when the value is 'y', the checkbox is checked, and when it's 'n', the
checkbox isn't checked.

I have tried ...

<asp:CheckBox ID="chkPhotosShow" Text=""Checked='<%#DataBinder.Eval(Container.DataItem, "ShowEvent")=="y"?true:false%>' Runat="Server" />

which shows the checkbox, but never checks it.

Any ideas what I'm doing wrong? TIA
 
A

Alan Silver

Any ideas what I'm doing wrong? TIA

I found an answer to this, so I'll post it here in case it's of use to
anyone.

The easiest way I found was to create a function :-

Boolean TrueIfY(string YN) {
if (YN == "y") {return true;}
else {return false;}
}

and use this in the ItemTemplate ...

<asp:CheckBox ID="chkPhotosShow" Text=""
Checked='<%#TrueIfY((string)DataBinder.Eval(Container.DataItem,
"ShowEvent"))%>' Runat="Server" />

This worked fine. I still can't work out *why* this works when the
previous code didn't, as both ways you seem to be passing a Boolean
value. Still, at least it works ;-)

Hope this is of use to someone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top