0

I am using an EntityDataSource with a DetailsView.

How can I get one of the nullable boolean fields to default to 'checked' when inserting new items?

<asp:CheckBoxField DataField="MyBoolColumn" HeaderText="Bool" />

I have tried setting a default value in the DB, a default value in the properties of the entity and by setting a default value in the constructor of the Entity:

    public partial class MyEntity
    {
    public MyEntity()
    {
           this._MyBoolColumn= true;
    }

1 Answer 1

1

If you want this field to be checked you must make it checked by default in your ASP.NET page or set it in the code behind (handle Inserting and Updating events in the data source) because any your change is overwritten by false from unchecked checkbox in the page. But setting any checkbox by default as true when it is not visible on the page is strange and very user unfriendly.

Sign up to request clarification or add additional context in comments.

4 Comments

But the CheckBoxField does not have a 'checked' or 'defaultvalue' property so I can't set it. There is also no ID property (so I can't use FindControl).
You can indeed find the check box and set its value to checked. Controls offers event which can allow you modifying databound record where you can find a control and set anything you need. For example GridView provides RowDataBound. I remember that we used it quite often when we worked with web forms.
This is a asp:CheckBoxField. It does not have an ID property. How can I use the FindControl method?
You can iterate controls in the cell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.