How to Get Values of Selected Items in Checkboxlist with Foreach in ASP.NET C#

How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?

Note that I prefer the code to be short.

List<ListItem> selected = CBLGold.Items.Cast<ListItem>()
.Where(li => li.Selected)
.ToList();

or with a simple foreach:

List<ListItem> selected = new List<ListItem>();
foreach (ListItem item in CBLGold.Items)
if (item.Selected) selected.Add(item);

If you just want the ListItem.Value:

List<string> selectedValues = CBLGold.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.Value)
.ToList();

Determining which items are selected in CheckBoxList using Request.Form

As you are binding the checkboxList from Database with OptionId feild you can check which checkboxes are selected and store their value in List<int> assuming their value is int.

Then again when you populate the checkboxlist you can check which value is present in the list stored earlier and based on value you can select the relevant checkbox.

Here is a sample code

List<int> SelectedCheckBoxes = new List<int>();
var selectedCheckBoxItems = from key in Request.Form.AllKeys
where key.Contains(cblAnimalType.ID)
select Request.Form.Get(key);
foreach (var item in selectedCheckBoxItems)
{
SelectedCheckBoxes.Add(int.Parse(item.ToString()));
}

Then again when you populate the checkboxlist you can find the items by value and select them

foreach (ListItem listItem in cblAnimalType.Items)
{
if (SelectedCheckBoxes.Contains((int.Parse(listItem.Value))))
{
listItem.Selected = true;
}
}

You may need to store the SelectedCheckBoxes List in session depending on how you are populating the checkboxlist.

Update

As per discussion with you you were using Asp.net 4.0 but your checkboxes didn't have a value attribute which should have been there.

This is happening because you are using controlRenderingCompatibilityVersion="3.5" which is no longer needed as you are already using Asp.Net 4.0.

So removing this line from web.congif will solve the issue and you will be able to get the value of checkbox instead of just getting on

<pages controlRenderingCompatibilityVersion="3.5" />

How to get value of CheckBoxList items other than text

When you string.Join(",", selectedInterest) - runtime will call .ToString() on every item in selectedInterest which in fact returns value of .Text property.

Instead you need to join ListItem.Value properties values. This can be done with LINQ:

var selectedInterest = ChkAreaOfInterest.Items.OfType<ListItem>().Where(i => i.Selected);
var sCheckedValue = string.Join(",", selectedInterest.Select(i => i.Value));

Getting CheckBoxList Item values

This ended up being quite simple. chBoxListTables.Item[i] is a string value, and an explicit convert allowed it to be loaded into a variable.
The following code works:

private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = (string)chBoxListTables.Items[i];
MessageBox.Show(str);
}
}
}

How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart

In your ASPX page you've got the list like this:

    <asp:CheckBoxList ID="YrChkBox" runat="server" 
onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
<asp:Button ID="button" runat="server" Text="Submit" />

In your code behind aspx.cs page, you have this:

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Populate the CheckBoxList items only when it's not a postback.
YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
YrChkBox.Items.Add(new ListItem("Item 2", "Item2"));
}
}

protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Create the list to store.
List<String> YrStrList = new List<string>();
// Loop through each item.
foreach (ListItem item in YrChkBox.Items)
{
if (item.Selected)
{
// If the item is selected, add the value to the list.
YrStrList.Add(item.Value);
}
else
{
// Item is not selected, do something else.
}
}
// Join the string together using the ; delimiter.
String YrStr = String.Join(";", YrStrList.ToArray());

// Write to the page the value.
Response.Write(String.Concat("Selected Items: ", YrStr));
}

Ensure you use the if (!IsPostBack) { } condition because if you load it every page refresh, it's actually destroying the data.

how to get selected item in CheckBoxList in Asp.net

You could go about this by taking the items of the checkbox list and converting them to ListItems and from that collection fetch those who is selected, like this:

var selectedItems = yourCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);

How to get value of selected checkboxlist items using javascript in asp.net

Try this :

<script type = "text/javascript">

function GetCheckBoxListValues(chkBoxID)
{
var chkBox = document.getElementById('<%= cblHotelFacility.ClientID %>');
var options = chkBox.getElementsByTagName('input');
var listOfSpans = chkBox.getElementsByTagName('span');
for (var i = 0; i < options.length; i++)
{
if(options[i].checked)
{
alert(listOfSpans[i].attributes["JSvalue"].value);
}
}
}

</script>

How to loop through a checkboxlist and to find what's checked and not checked?

for (int i = 0; i < clbIncludes.Items.Count; i++)
if (clbIncludes.GetItemChecked(i))
// Do selected stuff
else
// Do unselected stuff

If the the check is in indeterminate state, this will still return true. You may want to replace

if (clbIncludes.GetItemChecked(i))

with

if (clbIncludes.GetItemCheckState(i) == CheckState.Checked)

if you want to only include actually checked items.



Related Topics



Leave a reply



Submit