How do I do data binding with a checkbox?

Discussion in 'ASP .Net' started by Alan Silver, Feb 21, 2005.

  1. Alan Silver

    Alan Silver Guest

    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

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Feb 21, 2005
    #1
    1. Advertising

  2. Alan Silver

    Alan Silver Guest

    >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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Feb 22, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan
    Replies:
    2
    Views:
    2,518
    Jordan
    Feb 10, 2004
  2. tshad
    Replies:
    0
    Views:
    478
    tshad
    Apr 15, 2005
  3. cannontrodder

    Binding custom data object to checkbox list?

    cannontrodder, Aug 15, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    8,403
    cannontrodder
    Aug 15, 2006
  4. JcFx
    Replies:
    0
    Views:
    265
  5. Eric A. Miller

    Checkbox data binding on Checked property

    Eric A. Miller, Apr 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    1,080
    Elroyskimms
    Apr 5, 2006
Loading...

Share This Page