0

I need to bind a checkBox but with this line of code,

<asp:CheckBox ID="CheckBoxRed" runat="server"  Checked='<%# Bind("red") %>'/>

because initially the value in the DB is null, I get an invalid cast. I therefore tried in this way:

 <asp:CheckBox ID="CheckBoxRed" runat="server"  Checked='<%# (DataBinder.Eval(Container.DataItem,"red") is DBNull ?false:Eval("red")) %>'/>

This avoids the initial invalid cast but if I check the checkbox the value is not saved to the db. Is there a way to avoid the initial invalid cast and get the checked/unchecked status saved into the db?

I already checked this answer but it does not bind the value.

1
  • Does it work if you change Eval("red") to Bind("red") in your second version? If not, maybe try to convert nulls to false in DataSource before binding? Commented Jun 9, 2014 at 16:23

1 Answer 1

0

Try using this

<asp:CheckBox ID="CheckBoxRed" runat="server"  Checked='<%# Boolean.Parse(Eval("IsActive").ToString()) ? true : false%>'/>
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.