I need a two way databinding between a checkbox and a char field in the db that can contains Y or N.
The checkbox is within a FormView. Whenever the form is submitted, the bool state of the checkbox is translated into the corresponding Y or N field in the datasource and then committed to the database. I’m using LINQ to SQL Designer to generate the datasource code. Here’s what I had in mind using partials.
<asp:FormView ... datasource="DataSource1" EnableModelValidation="True" DefaultMode ="Edit">
<EditItemTemplate>
...
<asp:checkbox ... checked='<%# Bind("Enabled") %>'>
...
</EditItemTemplate>
</asp:FormView>
<asp:LinqDataSource ID="DataSource1" runat="server"
ContextTypeName="WebPages.WebPagesDataContext" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" TableName="Car">
</asp:LinqDataSource>
Then I can add my own Enabled property to the generated datasource using a partial. The partial class is in App_Code and in the same namespace as the linq datasource.
partial class Car
{
public bool Enabled
{
get { return DbEnabled == 'Y' }
set { DbEnabled = value ? 'Y' : 'N' }
}
}
I believe this is the correct way to do it. Currently the checkbox does reflect the state of the database field. However toggling the checkbox and submitting the form doesn’t result in a change in the DbEnabled field in the database.
How is this done?
Edit: The only thing missing to make this work was to make the property DbEnabled set to Private in the Linq to SQL designer.
You may create private column property and expose it via public property.
Here is an Emp entity,
And create the context,