2

Originally I had a TemplateField with a CheckBox in it but I couldn't set the checkbox Value using

Checked='<%# (bool)Bind("FieldName")  %>

or

Checked='<%# (bool)Eval("FieldName")  %>

It kept throwing an invalid cast exception. The field in the database is a bit field set to 1 or 0.

I tried switching to a checkbox field but because my update code is in the codebehind instead of using the updatecommand parameters in a datasource I can't seem to retrieve the value from the CheckBoxField to pass to my business logic.

Can any one point me on how to retrieve the value from the CheckBoxfield in a detailsview?

gsirianni
  • 1,334
  • 2
  • 18
  • 35

3 Answers3

1

I think that you need to use ToString() on the value from the database, since the HTML is looking for True or False, not 1 or 0.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
1

You can't cast from int to bool. Try Convert.ToBoolean(Eval("FieldName")) Not sure if you can use Eval like that though, but that's the general idea.

Davy8
  • 30,868
  • 25
  • 115
  • 173
0

Could you use

CheckBox ckBox = ucDetailView.FindControl("CheckBoxID") as CheckBox
if(ckBox != null){
   ckBox.Checked = (bool)datasource["FieldName"].ToString()
   //.. or some better casting code with more null checks
}

Then set it from there in code behind. Solution seemed to work here

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
  • I had something like this originally but I was utilizing a checboxfield as an alternate. However because the suggestion that I accepted above worked I can continue to use this method that you mentioned. – gsirianni Dec 02 '11 at 18:23