Checkbox data binding on Checked property

Discussion in 'ASP .Net Web Controls' started by Eric A. Miller, Apr 4, 2006.

  1. 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
    Eric A. Miller, Apr 4, 2006
    #1
    1. Advertising

  2. 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
    >
    >
    >
    Phillip Williams, Apr 5, 2006
    #2
    1. Advertising

  3. 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.)
    Steven Cheng[MSFT], Apr 5, 2006
    #3
  4. 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]" <> wrote in message
    news:...
    > 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.)
    >
    Eric A. Miller, Apr 5, 2006
    #4
  5. Eric A. Miller

    Elroyskimms Guest

    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.
    Elroyskimms, Apr 5, 2006
    #5
    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. Scott P.
    Replies:
    0
    Views:
    521
    Scott P.
    Apr 2, 2004
  2. yurps
    Replies:
    1
    Views:
    6,860
    rajeshthangarasu1983
    Feb 29, 2008
  3. Jon
    Replies:
    5
    Views:
    186
    Ken Cox [Microsoft MVP]
    Sep 18, 2003
  4. randy
    Replies:
    13
    Views:
    427
    Tad McClellan
    Nov 1, 2003
  5. Replies:
    3
    Views:
    145
    Frank
    Oct 6, 2005
Loading...

Share This Page