This is a migrated thread and some comments may be shown as answers.

Bind checkbox to none boolean database field

2 Answers 251 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Helen
Top achievements
Rank 1
Helen asked on 16 Apr 2014, 07:08 PM

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 Answers, 1 is accepted

Sort by
0
Helen
Top achievements
Rank 1
answered on 16 Apr 2014, 07:36 PM
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
0
Eyup
Telerik team
answered on 21 Apr 2014, 07:14 AM
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.

 
Tags
ListView
Asked by
Helen
Top achievements
Rank 1
Answers by
Helen
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or