Two-way Binding To CheckBox in GridView (ASP.Net 2.0)

Discussion in 'ASP .Net Web Controls' started by ES, Sep 14, 2006.

  1. ES

    ES Guest

    Hello,
    I have (logically) Boolean column "IsRequired" in a database table I'd like to show in a GridView. In the normal display, I'd like to show "Yes" or "No", and in the edit mode I'd like to bind the column to a CheckBox. The database column is an int with a 0 or 1.

    I've created a template column and in the ItemTemplate I call a "Friendly" Boolean conversion to display yes or no.
    Text='<%#FriendlyBoolean(Convert.ToBoolean(Eval("IsRequired"))) %>'

    So far, so good. However in the EditItemTemplate, I have to bind the <asp:CheckBox> control in such a way I get the value back to save.
    Checked='<%#Convert.ToBoolean(Eval("IsRequired")) %>'

    Works except of course Eval is one-way.

    Checked='<%#Convert.ToBoolean(Bind("IsRequired")) %>'

    and variations on that don't compile.

    I've tried changing the sql to give me 'true' or 'false'
    Select IsRequired Case When 0 then 'false' Else 'true' End As IsRequired ....
    and then binding directly to that:
    Checked='<%#Bind("IsRequired")%>
    however I get a type conversion error because of course sql is returning a string of true of false, not a true Boolean. I also get a type conversion error binding to the int. (I figured as much, but it was worth a try!)

    *sigh*

    A <asp:CheckBoxField> will bind to the column fine, but in the non-edit mode it displays as a faint, hard-to-see grayed out checkbox. It doesn't want to live in an <EditItemTemplate> either, so this has to be a non-templated column. If I could get it to display in black in non-edit mode, I'd live with it. I've tried
    ControlStyle-ForeColor="black"
    but this doesn't change the appearance. Apparently the disabled="disabled" that .Net renders for this control makes the color setting moot.

    This is what is rendered for the checkbox in "normal" mode.
    <span disabled="disabled" style="color:Black;"><input id="_ctl0_ph1_GV_A__ctl4__ctl3" type="checkbox" name="_ctl0:ph1:GV_A:_ctl4:_ctl3" checked="checked" disabled="disabled" /></span>

    Can anyone suggest a way to either
    a) two-way bind a database column to a regular checkbox control or
    b) Get the CheckBoxField to display black when not in edit mode?

    Thanks
    Elton
    ES, Sep 14, 2006
    #1
    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. Amit
    Replies:
    6
    Views:
    13,782
    Assimalyst
    Oct 24, 2006
  2. witek
    Replies:
    0
    Views:
    9,012
    witek
    Jul 18, 2006
  3. novus
    Replies:
    9
    Views:
    234
    last component
    Dec 14, 2005
  4. Ludovic DE FREITAS

    ASP.Net 2 Two Way Binding and DateTime Format Problem

    Ludovic DE FREITAS, Jan 9, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    1,435
    Phillip Williams
    Jan 9, 2006
  5. Bill Nicholson
    Replies:
    1
    Views:
    133
    Phil H
    Sep 26, 2007
Loading...

Share This Page