This is a migrated thread and some comments may be shown as answers.

ListBox.Items only returns the first

2 Answers 97 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Pedro
Top achievements
Rank 1
Pedro asked on 23 Jan 2017, 03:44 PM

Hi, I have a RadListBox that is being populated on Page_Load through a DataTable that comes from the DB. Problem is: when I try to iterate the ItemCollection (via the Items Property) the List has Count=1 (the first item), when it was supposed to be 20+(I don't know the exact item count). Below are the relevant snippets:

Here the list is iterated to find out what items are selected in the db and move them from this list to another.

protected void ListaPaisExp_ItemDataBound(object sender, RadListBoxItemEventArgs e)
        {
            foreach (DataRow dr in Empresa.Tables[1].Rows)
            {
                foreach (RadListBoxItem item in ListaPaisExp.Items)
                {
                    if (item.Value == dr["tipopaisid"].ToString())
                    {
                        ListaPaisExpSel.Items.Add(item);
                        item.Remove();
                    }
                }
            }
        }

 

And this is the DataBind on Page_Load:

ListaPaisExp.DataSource = Tipos_DAL.GetTiposByArchtype("pais");
ListaPaisExp.DataValueField = "ID";
ListaPaisExp.DataTextField = "Descricao";
ListaPaisExp.DataBind();

2 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 26 Jan 2017, 12:31 PM
Hello Pedro,

I am not quite sure about the logic implemented in the ItemDataBound event handler that you demonstrate. Please note that this event is triggered for each item from the datasource of the RadListBox.

With that said, as I can see, you iterate trough a table content and in some cases you remove the items that the DataBound event is triggered for.

Please described in details your business logic that needs to be implemented with the RadListBoxes so we could attempt to suggest you the proper implementation.

Regards,
Nencho
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Pedro
Top achievements
Rank 1
answered on 26 Jan 2017, 12:40 PM

Ah, I wasn't aware that the event was triggered on every single item, so probably I will play around with the events to see which ones fits the bill.

Regardless of that, my point with that code was to iterate the bound ListBox, and "move" (remove from one list and add to the other) the items selected in the DB to to the a second (empty and not bound) ListBox so that when this logic is done I have the add-able items on the first list and the existing items on the second list.

Tags
ListBox
Asked by
Pedro
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Pedro
Top achievements
Rank 1
Share this question
or