3

I need to bind my checkbox somewhere even if it's null 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 remove this code, it works, but no checkbox visible ofc.) Maybe instantiate it with a value set to it already (true , false)

Can anyone help please? Will reply in comment if needed.

1 Answer 1

6

use bool.Parse().

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

Or this is what you are looking for:

Checked='<%# Eval("DeleteFlag") == DBNull.Value ? false :  Eval("DeleteFlag") %>'
Sign up to request clarification or add additional context in comments.

Comments

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.