0

I have a checkbox list which is getting its list items from a database using the binding as shown in the snippet below:

 CheckBoxList1.DataSource = GetValues();
 CheckBoxList1.DataBind();

GetValues():

    var query = tran_DContext.get_ImportTabs();
    foreach (var item in query)
    {
        TabList.Add(item.TabName);
    }

    return TabList;

The above snippet successfully retrieves the column TabName and places it in the checkboxlist. But when the checkbox is selected on the website, the 'selection' value doesn't flow to my code. Am i missing something? i have the following code:

 private List<string> SelectedTabs()
{
    string strTemp = string.Empty;
    List<string> Seltabs = new List<string>();

    foreach (ListItem lst in CheckBoxList1.Items)
    {
        if (lst.Selected)
            Seltabs.Add(CheckBoxList1.SelectedValue);
    }
    return Seltabs;
}

help?

1 Answer 1

3

The problem exists in your foreach loop. Calling CheckBoxList1.SelectedValue when multiple values are selected will result in the first selected value being returned.

Try this instead:

foreach (ListItem lst in CheckBoxList1.Items)
{
    if (lst.Selected)
        Seltabs.Add(lst.Value);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.