Add Default Item in RadCombo

5 posts, 0 answers
  1. Amol
    Amol avatar
    57 posts
    Member since:
    Apr 2008

    Posted 11 Dec 2008 Link to this post

    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

  2. Amol
    Amol avatar
    57 posts
    Member since:
    Apr 2008

    Posted 14 Dec 2008 Link to this post

    any comments?

    Thanks,
    Amol
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. andy
    andy avatar
    19 posts
    Member since:
    Jun 2008

    Posted 15 Dec 2008 Link to this post

    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")); 
  5. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 16 Dec 2008 Link to this post

    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.
  6. Amol
    Amol avatar
    57 posts
    Member since:
    Apr 2008

    Posted 16 Dec 2008 Link to this post

    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")); 
                    } 
     
    ....... 
     

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017