Checkbox data binding on Checked property

E

Eric A. Miller

I have a checkbox that is bound to a database column. The values in the
database column are either 'Y' or 'N'. I'm having trouble figuring out how
to write the Bind method to convert the 'Y' or 'N' to a boolean so it can be
assigned to the Checked property of the checkbox. Two-way binding is
required for the checkbox and it is a template column in the
EditItemTemplate.

I've tried: Bind("column_name" == 'Y' ? true : false) with no success.

Any ideas?

Thank you.

Eric
 
P

Phillip Williams

I tend to believe that you cannot do a 2-way databinding where you have to
change the field values from one type to another.

I would have replaced it with one-way Eval("ColumnName")=="Y" and then
consumed the GridView.RowUpdated event or the FormView.ItemUpdated event to
set the update parameters NewValues with the correct value, e.g.
e.NewValues["FieldName"] =
FormView1.FindControl("chkFieldname").Checked?"Y":"N";
 
S

Steven Cheng[MSFT]

Thanks for Phillip's input.

Hi Eric,

I think you need to use the "Eval" expression instead of the "Bind" since
"Bind" is used for two-way databinding and has limited functionalty. For
your scenario, you just need to use "Eval" to extract the column value and
compare it to convert it to boolean value. e.g;

<%# Eval("CategoryName").ToString() == "TestName"? true: false %>

Also, we can also define a helper function in page's code behind and call
the helperfunction in databinding expression to do th work. e.g;

<%# GetBool(Eval("CategoryName")) %><br />

========in code behind============
protected bool GetBool(object obj)
{
return obj.ToString() == "TestName" ? true : false;
}


Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
E

Eric A. Miller

Thank you Phillip and Steven for your replies. It's comforting to know that
there is a reason why I couldn't get the Bind to work.

Take care.

Eric
 
E

Elroyskimms

Eric,

Another option would be to make the change to your data in the stored
procedure or query you are using to create it. In SQL 2000, you could
use the REPLACE command to convert your Y|N to 1|0.

Here is some untested sample code for SQL 2000:
SELECT Description, REPLACE(REPLACE(IsChecked,'N',0),'Y',1) as
CheckedValue
FROM MyTable

If you are not using SQL, I'm sure there is something similar in your
DB. If your Checkbox list is really long, this may speed things up a
bit. Although the performance gain might be minimal, this would prevent
your system from having to run the Eval code for every item in the
Checkbox list control.
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top