Binding checkbox, even if null (date-driven)

I need to snap my flag somewhere, even if it is invalid, so I won’t get an error about invalid casting.

            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("DeleteFlag", typeof(bool));

In ASP:

    <asp:CheckBox ID="MyCheckBox" runat="server" Checked='<%# Bind("DeleteFlag") %>'
                    Enabled="False"/>

(If I delete this code, it works, but the checkbox does not appear.) Perhaps create an instance with the value already set (true, false)

Can anyone help? If necessary, reply in the comments.

+3
source share
1 answer

use bool.Parse ().

   <asp:CheckBox ID="MyCheckBox" runat="server"
Checked='<%#bool.Parse(Eval("DeleteFlag").ToString())%>'
                        Enabled="False"/>

Or is this what you are looking for:

Checked='<%# Eval("DeleteFlag") == DBNull.Value ? false :  Eval("DeleteFlag") %>'
+5
source

All Articles