Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Checkbox data binding on Checked property

Reply
Thread Tools

Checkbox data binding on Checked property

 
 
Eric A. Miller
Guest
Posts: n/a
 
      04-04-2006
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


 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      04-04-2006
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";


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"Eric A. Miller" wrote:

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

 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      04-05-2006
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.)

 
Reply With Quote
 
Eric A. Miller
Guest
Posts: n/a
 
      04-05-2006
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
"Steven Cheng[MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.)
>



 
Reply With Quote
 
Elroyskimms
Guest
Posts: n/a
 
      04-05-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CheckBox Checked=false when checked in DataList yurps ASP .Net 1 02-29-2008 01:08 PM
checkbox checked property "undefined" in IE frank.cleuren@gmail.com Javascript 3 10-06-2005 03:29 PM
Problem Changing the Checked Property of a dynamic checkbox Scott P. ASP .Net 0 04-02-2004 05:12 AM
Checkbox - database checkbox, if checked gives value of 1 .. how to sum ? randy Perl Misc 13 11-01-2003 05:39 AM
Unable to detect Checked property of Checkbox in Datagrid Jon ASP .Net Web Controls 5 09-18-2003 10:54 PM



Advertisments