Selecting Multiple Items From CheckBoxList RRS feed

  • Question

  • i am trying to get multiple selected items from a asp:CheckBoxList but when i try to compare selected items with some value it says indexing is not allowed. here is the code and Underlined line contained errored code

          // Cat_check is a CheckBoxList in which Multiple items are selected
            for (int i = 0; i < cat_check.Items.Count; i++)
                if (cat_check.SelectedItem != null)
                        string columnName = cat_check.SelectedItems[i].Text;
                       var column = gvRecoed.Columns.Cast<DataControlField>()
                          .FirstOrDefault(c => c.HeaderText == columnName);
                        if (column != null) column.Visible = true;

    the idea is that the gvRecoed(GridView) column headers will be matched with CheckBoxList items and only matched columns will be set to visible true..

    Friday, November 29, 2013 6:31 AM


  • Hi Adil,

    I cannot find the SelectedItems property in the CheckBoxList class properties list. I believe this is the reason of this issue. We can use the following code to instead of your code.

    string columnName = cat_check.Items[i].Text;

    To meet your requirement, I mark some changes on your code like below:

    for (int i = 0; i < cat_check.Items.Count; i++)


                    if (cat_check.Items[i].Selected)


                        gvRecoed.Columns[i].Visible = true;


                    else {

                        gvRecoed.Columns[i].Visible = false;



    And here is the like of the whole project about it.


    By the way, this thread is specific to ASP. NET forum. This form is to discuss problems about CLR development. Please post a new thread on that forum for more effective response. http://forums.asp.net/.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 2, 2013 5:48 AM