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;
    }
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
simon831
  • 5,166
  • 7
  • 33
  • 50

1 Answers1

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 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). – simon831 May 31 '11 at 14:42
  • 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](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx). I remember that we used it quite often when we worked with web forms. – Ladislav Mrnka May 31 '11 at 14:47
  • This is a asp:CheckBoxField. It does not have an ID property. How can I use the FindControl method? – simon831 May 31 '11 at 16:25
  • You can iterate controls in the cell. – Ladislav Mrnka May 31 '11 at 16:28