[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Populating Check Box List via Code

Trying to assign values from a dataset to a checkbox list control. I create the dataset (ds), fill it, then use the following line of code in a loop:

CheckBoxList1.Items.Insert(0, ds.Tables(0).Rows(X).Item(1)).ToString()

This code populates the CheckBox List with the correct items. However, I want to associate a selected value (integer) with the item (string). the value is at ds.Tables(0).Rows(X).Item(2)) so, for example, one option has selectedItem of Pencil, while the selectedValue of pencil is 2. The one line of code, as written, fills the Value and Item properties with the same data.

  • 2
1 Solution
HainKurtSr. System AnalystCommented:
try this:

CheckBoxList1.Items.Insert(0, new ListItem(ds.Tables(0).Rows(X).Item(1)).ToString(), ds.Tables(0).Rows(X).Item(2)).ToString())
HainKurtSr. System AnalystCommented:
for excample this is what I tested and working fine

        CheckBoxList1.Items.Insert(0, New ListItem("select a city", 0))
        CheckBoxList1.Items.Insert(0, New ListItem("Istanbul", 1))
        CheckBoxList1.Items.Insert(0, New ListItem("Ankara", 2))
        CheckBoxList1.Items.Insert(0, New ListItem("Gaziantep", 3))

first item is the text, the second is the value when selected
john03063Author Commented:
Thanks! I knew it had to be a simple thing!!


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now