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

Adding a default option to RadComboBox

1 Answer 223 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 03 Mar 2010, 06:07 PM
I am using a RadComboBox with EnableLoadOnDemand="true", and OnItemsRequested="brand_ItemsRequested" - I want to add a default item that appears at the top of the list, so I have added comboBrand.Items.Insert(0, new RadComboBoxItem("All Brands", "0")); 

protected void brand_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e) 
        { 
            DataTable data = GetData(e.Text); 
 
            int itemOffset = e.NumberOfItems; 
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, data.Rows.Count); 
            e.EndOfItems = endOffset == data.Rows.Count; 
 
            for (int i = itemOffset; i < endOffset; i++) 
            { 
                comboBrand.Items.Add(new RadComboBoxItem(data.Rows[i]["Name"].ToString(), data.Rows[i]["Name"].ToString())); 
            } 
 
            comboBrand.Items.Insert(0, new RadComboBoxItem("All Brands""0"));  
            e.Message = GetStatusMessage(endOffset, data.Rows.Count); 
        } 

<telerik:RadComboBox ID="comboBrand" AppendDataBoundItems="true"  LoadingMessage="Retrieving Brands..." AutoPostBack="true" runat="server" Width="250px" Height="150px" 
            EnableLoadOnDemand="true" ShowMoreResultsBox="true" ShowWhileLoading="false"  
            EnableVirtualScrolling="true" OnSelectedIndexChanged="comboBrand_SelectedIndexChanged" 
            OnItemsRequested="brand_ItemsRequested">  
        </telerik:RadComboBox> 


The trouble is, each time more results load and appends to the list, "All Brands" is also appended to the results that are returned, so it appears every 12th row. How can I insert just a single default value at the top of the list?

EDIT:

I tried wrapping some logic around the the  comboBrand.items.insert to check for the item before inserting the default item, but multiple items are still being added:

if (!comboBrand.Items.Contains(new RadComboBoxItem("All Brands""All Brands"))) 
            { 
                comboBrand.Items.Insert(0, new RadComboBoxItem("All Brands""All Brands")); 
            } 

1 Answer, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 04 Mar 2010, 04:40 PM
Hello Neil,

Your approach does not work because Items already loaded in the RadComboBox are not available on subsequent Items request, i.e. they exist only on the client-side.

Alternatively, you can verify e.NumberOfItems (which holds the number of Items currently loaded on the client-side) and add the default Item only the value is 0.
 
Best wishes,
Simon
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
ComboBox
Asked by
Neil
Top achievements
Rank 1
Answers by
Simon
Telerik team
Share this question
or