Home All Groups Group Topic Archive Search About

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

Author
14 Sep 2006 7:33 PM
ES
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

Bookmark and Share