0

I am reading values from a database (boolean) and if true, I want it to check a checkbox, and if false, keep the checkbox unchecked. The lines look like this: cblEnergy.Items.FindByValue(1).Selected = track_usage

In this case, track_usage is a boolean value.

This way does not actually create the check mark in the checkbox like I wanted it to. What is the best way to do this?

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Kruug
  • 449
  • 2
  • 9
  • 20
  • Than what happens that you did not expect? – Magnus May 16 '12 at 20:54
  • Is your track_usage = false? :-) – Steve May 16 '12 at 21:13
  • I want the check box to have the check and be viewed as selected for other parts of the program. I expected that by setting it to true, it would be true, but it's not. – Kruug May 16 '12 at 21:14
  • @Steve -> No, it is not false – Kruug May 16 '12 at 21:15
  • 1
    FindByValue defaults to look for a string value. Passing it 1 might not give you the result you are looking for. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection.findbyvalue.aspx – A. Still May 16 '12 at 21:35
  • @A.Still The `1` is a String. I changed the values to numbers to mirror the physical form I'm developing from. Do you know how I would go about getting the result I'm looking for? – Kruug May 16 '12 at 21:43
  • Why can't you use the `checkbox.checked =true/false` property? you should post more code to tell us what variables you are using. – Subs May 17 '12 at 02:48
  • @Subs I don't have a `checkbox.checked` property. Code here: http://pastebin.ca/2149545 – Kruug May 17 '12 at 03:03
  • I think you are using checkedlistbox. In that case to check an item at say index 1, you would say `CheckedListBox1.SetItemChecked(1, True)` – Subs May 17 '12 at 03:12
  • @Subs I don't have `CheckedListBox1.SetItemChecked()`. I only have `SetMetaTable` and `SetRenderMethodDelegate`. – Kruug May 17 '12 at 04:05
  • Please post your `Form` details and tell me which checkbox you are trying to check/uncheck. – Subs May 17 '12 at 04:12
  • @Subs vb code here: http://pastebin.ca/2149545 html here: http://pastebin.ca/2149566 – Kruug May 17 '12 at 04:21
  • @Subs I'm trying to check a box or leave it unchecked based on the boolean data that comes out of the database. – Kruug May 17 '12 at 04:21
  • Have you tried using `cblEnergy.Items.FindByValue(1).Checked = track_usage`.. – Writwick May 17 '12 at 04:42
  • @I.am.WritZ I don't have a `Checked` option – Kruug May 17 '12 at 04:43
  • @Kruug The code you posted doesn't give any information about the controls you use on Form. You should also point out where your checkbox is in that code. – Subs May 17 '12 at 04:49
  • @Subs There's many. Every time it states `` – Kruug May 17 '12 at 04:52
  • Ok! the Code I found working is `cblEnergy.SetItemChecked(1, track_usage)`...Please Check This Code Out.. – Writwick May 17 '12 at 04:56
  • @I.am.WritZ Subs already pointed that out...not available. – Kruug May 17 '12 at 05:01
  • Yes I saw that..But there is no other answer available at the moment else that one.. Can You Do `cblEnergy.Items(1).Selected = track_usage`?? – Writwick May 17 '12 at 05:02
  • @I.am.WritZ Still did not work. I'm using Visual Studio 2010 and these are Web Form's in Visual Basic. I don't know if I don't have the right modules available or something, but it's really starting to frustrate me... – Kruug May 17 '12 at 05:26
  • The last hope is to use `cblEnergy.Items.Item(1).Selected = track_usage` as mentioned [Here](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection.item) – Writwick May 17 '12 at 05:35

1 Answers1

0

If cb1Energy is CheckBoxList then you would use:

  Dim li_chkbox = cblEnergy.Items.FindByValue("1")
  If (li_chkbox != null) then
     li_chkbox.Selected = track_usage
  End if
Subs
  • 529
  • 2
  • 9
  • I'm not seeing how this is different than how I have it...care to explain? – Kruug May 17 '12 at 05:42
  • It all depends if you can show me what code you wrote in `` – Subs May 17 '12 at 05:44
  • Honestly, all of the code is posted on pastebin. My VB code: http://pastebin.ca/2149545 The HTML: http://pastebin.ca/2149566 – Kruug May 17 '12 at 05:45
  • Well, now i see the new link 149566 – Subs May 17 '12 at 05:49
  • ` ` ` Track electricity usage, fuel usage, and natural gas usage and share data with employees` ` Develop a policy/procedure strategy to conserve energy/increase renewable energy use` You did use a string - `"1"` , `"2"` in the ListItem Value. Try the change and see whether it works. – Subs May 17 '12 at 05:52
  • Facepalm mode initiated...testing it now. – Kruug May 17 '12 at 05:54
  • Well, it worked as `Value(1)` when I was writing to the database. It still doesn't work as `Value("1")` when reading from the database. – Kruug May 17 '12 at 05:57
  • maybe your `track_usage` variable is `false`. You can test it out giving `True` – Subs May 17 '12 at 06:00
  • alright...this is irritating...when hard coding `True` it works. When I grab a boolean value from the database, it doesn't work...VB automatically turns a 1 into True, correct? – Kruug May 17 '12 at 06:03
  • see this link - http://stackoverflow.com/questions/3621037/casting-a-boolean-to-an-integer-returns-1-for-true – Subs May 17 '12 at 06:11