Getting CheckBoxList Item values

前端 未结 8 819
野的像风
野的像风 2021-01-04 10:53

I have a CheckBoxList which I\'m populating with data. When I attempt to retrieve the checked items from the list I can only grab the item ordinal, I cannot get the value.

8条回答
  •  半阙折子戏
    2021-01-04 11:22

    Try to use this.

            for (int i = 0; i < chBoxListTables.Items.Count; i++)
            {
                if (chBoxListTables.Items[i].Selected)
                {
                    string str = chBoxListTables.Items[i].Text;
                    MessageBox.Show(str);
    
                    var itemValue = chBoxListTables.Items[i].Value;
                }
            }
    

    The "V" should be in CAPS in Value.

    Here is another code example used in WinForm app and runs properly.

            var chBoxList= new CheckedListBox();
            chBoxList.Items.Add(new ListItem("One", "1"));
            chBoxList.Items.Add(new ListItem("Two", "2"));
            chBoxList.SetItemChecked(1, true);
    
            var checkedItems = chBoxList.CheckedItems;
            var chkText = ((ListItem)checkedItems[0]).Text;
            var chkValue = ((ListItem)checkedItems[0]).Value;
            MessageBox.Show(chkText);
            MessageBox.Show(chkValue);
    

提交回复
热议问题