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

Add Default Item in RadCombo

4 Answers 124 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Amol
Top achievements
Rank 2
Amol asked on 11 Dec 2008, 11:56 AM

Hi all,

I am using radcombo with OnItemsRequested event,
I want to add one default item say <Unknown> in to combo,

i tried to add as below, but it displays <Unknown> item many times. Item is added in each request.

<telerik:RadComboBox ID="rcbBeginPeriod" runat="server"  
                    Width="250px" Height="100px" Skin="Office2007"  
                    AllowCustomText="false"  
                    ShowToggleImage="true"  
                    ShowMoreResultsBox="True" 
                    EnableLoadOnDemand="True"  
                    MarkFirstMatch="True"  
                    OnItemsRequested="rcbBeginPeriod_ItemsRequested"  
                    EnableVirtualScrolling="true"                     
                    
                    > 
                </telerik:RadComboBox> 

protected void rcbBeginPeriod_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e) 
    { 
        try 
        { 
 
            EventInfo objEventInfo = new EventInfo(); 
 
            List<ctblPeriod> resultSet = null
            resultSet = objEventInfo.GetAllQuarterPeriods(e.Text); 
             
 
            try 
            { 
 
                int itemsPerRequest = PRESCONSTANTS.COMBO_ITEMS_PER_REQUEST; 
                int itemOffset = e.NumberOfItems; 
                int endOffset = itemOffset + itemsPerRequest; 
                if (endOffset > resultSet.Count) 
                { 
                    endOffset = resultSet.Count; 
                } 
                if (endOffset == resultSet.Count) 
                { 
                    e.EndOfItems = true
                } 
                else 
                { 
                    e.EndOfItems = false
                } 
                for (int i = itemOffset; i < endOffset; i++) 
                { 
                    rcbBeginPeriod.Items.Add(new RadComboBoxItem(resultSet[i].Period, resultSet[i].ID.ToString())); 
                } 
 
                if (resultSet.Count > 0) 
                { 
                    lblBeginPeriodID.Text = ""
                    e.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), resultSet.Count.ToString()); 
                } 
                else 
                { 
                    lblBeginPeriodID.Text = ""
                    e.Message = "No matches"
                } 
            } 
            catch 
            { 
                lblBeginPeriodID.Text = ""
                e.Message = "No matches"
            } 
            finally 
            { 
                if (!rcbBeginPeriod.Items.Contains(new RadComboBoxItem("<Unknown>""-1"))) 
                    rcbBeginPeriod.Items.Add(new RadComboBoxItem("<Unknown>""-1")); 
            } 
 
        } 
        catch (Exception ex) 
        { 
             
        } 
    } 
 


Please help me to solve this problem.

Thanks
Amol

4 Answers, 1 is accepted

Sort by
0
Amol
Top achievements
Rank 2
answered on 15 Dec 2008, 04:09 AM
any comments?

Thanks,
Amol
0
andy
Top achievements
Rank 1
answered on 15 Dec 2008, 06:25 PM
Hi Amol,

index -1 can only be used for Adding, after added, it's no longer -1,
use (item.Count - 1 ) for the last index

try this

 if (!rcbBeginPeriod.Items.Contains(new RadComboBoxItem("<Unknown>",rcbBeginPeriod.Items.count - 1 ))) 
                    rcbBeginPeriod.Items.Add(new RadComboBoxItem("<Unknown>""-1")); 
0
Simon
Telerik team
answered on 16 Dec 2008, 06:07 PM
Hi Amol,

Your approach would not work because Items that are loaded in the ComboBox are not sent back to the server and subsequently to the ItemsRequested event handler.

So, upon each callback the 'Unknown' Item will not be found in the collection and will be added again.

In order to work around this behavior please do the following:
  1. Add the Item on the first page load:
    if ( ! this.IsPostBack) 
        this.rcbBeginPeriod.Items.Add(new RadComboBoxItem("<Unknown>", "-1")); 
  2. Clear the existing Items in the ComboBox in the beginning of the ItemsRequested event handler:
    protected void rcbBeginPeriod_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e) 
        { 
            this.rcbBeginPeriod.Items.Clear(); 
Regards,
Simon
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Amol
Top achievements
Rank 2
answered on 17 Dec 2008, 04:14 AM
Thanks Andy and Simon.

I have added item in ItemsRequested event handler  as follows and it works fine.

protected void rcbBeginPeriod_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e) 
    { 
         
             
            EventInfo objEventInfo = new EventInfo(); 
            rcbBeginPeriod.Items.Clear(); 
            List<ctblPeriod> resultSet = null
            resultSet = objEventInfo.GetAllQuarterPeriods(e.Text); 
            
 
                int itemsPerRequest = 10
                int itemOffset = e.NumberOfItems; 
                int endOffset = itemOffset + itemsPerRequest; 
 
                if (endOffset == itemsPerRequest) 
                { 
                    rcbBeginPeriod.Items.Add(new RadComboBoxItem("<Unknown>", "-1")); 
                } 
 
....... 
 

Tags
ComboBox
Asked by
Amol
Top achievements
Rank 2
Answers by
Amol
Top achievements
Rank 2
andy
Top achievements
Rank 1
Simon
Telerik team
Share this question
or