Bind checkbox to none boolean database field

3 posts, 0 answers
  1. Helen
    Helen avatar
    42 posts
    Member since:
    Nov 2013

    Posted 16 Apr 2014 Link to this post


    Hi,

    I want to have a checkbox that binds to a field which stores 'Y' or 'N' instead of boolean value.  Is there an event which I can put the codes to alter true/false back to 'Y'/'N' before saving back to the database? Is this even possible?

    In the ItemTemplate I am using label to display the content of the field, so it has no problem displaying. In the EditItemTemplate, here's what I'm doing:

    aspx:
    <asp:CheckBox runat="server" Text="Vendor" ID="ThirdPartyVendorCheckbox" />
     <asp:HiddenField runat="server" ID="hiddenThirdPartyVendor" Value='<%# Eval("thirdparty_vendor") %>' />

    Code-Behind:

    protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
     
    {
     
        if (e.Item is RadListViewEditableItem && e.Item.IsInEditMode)
     
        {
     
            CheckBox chkbox = e.Item.FindControl("ThirdPartyVendorCheckbox") as CheckBox;
     
            HiddenField hf = e.Item.FindControl("hiddenThirdPartyVendor") as HiddenField;
     
            string txtVal = hf.Value;
     
            if (txtVal != "Y")
     
                chkbox.Checked = true;
     
            else
     
                chkbox.Checked = false;
     
        }
     
    }


    Thank you,

    Helen

  2. Helen
    Helen avatar
    42 posts
    Member since:
    Nov 2013

    Posted 16 Apr 2014 Link to this post

    I think I got it ... please let me know if you think there's gonna be a problem.

    In aspx, I changed the hidden field to bind instead of eval and set the 'checked' property of the checkbox in the code-behind.

    <asp:CheckBox runat="server" Text="Vendor"  ID="ThirdPartyVendorCheckbox" />
     
    <asp:HiddenField runat="server" ID="hiddenThirdPartyVendor" Value='<%# Bind("thirdparty_vendor") %>' />

    Then in code behind, add the logic in ItemUpdating event
    protected void RadListView1_ItemUpdating(object sender, RadListViewCommandEventArgs e)
    {
        CheckBox chkbox = e.ListViewItem.FindControl("ThirdPartyVendorCheckbox") as CheckBox;
        HiddenField hf = e.ListViewItem.FindControl("hiddenThirdPartyVendor") as HiddenField;
     
        if (chkbox.Checked)
            hf.Value = "Y";
        else
            hf.Value = "N";
    }


    Regards,

    Helen
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 21 Apr 2014 Link to this post

    Hello Helen,

    I'm glad you've managed to find a viable solution for your scenario.
    The approach seems fine and in case you have any further difficulties with its implementation, please feel free to turn to us.

    Regards,
    Eyup
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top