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

LoadOnDemand and preloaded data

2 Answers 163 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
wdudek
Top achievements
Rank 1
wdudek asked on 20 Dec 2011, 08:44 PM
I need to pre load some data into a combo box and select an item, then use the load on demand functionality to add additional items (There are many dollar amount options, but usually a low one is selected so I don't want to take the hit of adding all of them unless I need to). If I don't preload it works fine, but when I load some data and then add more I seem to get the inital data added again and again. In the following example I get

1. numbers 1-10 loaded
2. numbers 1-20 added to the list
3. numbers 1-10 and then 31 - 40

Basically how can I prevent it from reloading the 1 - 10 items each time?? It seems to happen since it was created on page load

aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="TestHome.aspx.cs" Inherits="_Default" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
 
        <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="200px" Width="300px">
 
            <telerik:RadComboBox ID="cbTest" runat="server" EnableLoadOnDemand="true" ShowMoreResultsBox="true" EnableVirtualScrolling="true" OnItemsRequested="cbTestItemsRequested">
            </telerik:RadComboBox>
 
        </telerik:RadAjaxPanel>
     
         
    </div>
    </form>
</body>
</html>

Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Telerik.Web.UI;
 
public partial class _Default : System.Web.UI.Page
{
    int _itemsPerRequest = 10;
     
    protected void Page_Load(object sender, EventArgs e)
    {
 
         
        if (!Page.IsPostBack)
        {
            List<int> data = GetData();
 
            for (int i = 0; i < 10; i++)
                cbTest.Items.Add(new RadComboBoxItem(data[i].ToString()));
 
 
            cbTest.FindItemByText("5").Selected = true;
        }
         
    }
 
 
 
    protected void cbTestItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        List<int> data = GetData();
 
        int start = e.NumberOfItems;
 
        int count =  _itemsPerRequest;
 
        if (count > data.Count - start)
            count = data.Count - start;
 
         
 
         
        var temp = data.Skip(start).Take(count);
 
        foreach (var v in temp)
            cbTest.Items.Add(new RadComboBoxItem(v.ToString()));
 
        if (start + count == data.Count)
            e.EndOfItems = true;
    }
 
    protected void OldcbTestItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
         
 
        List<int> data = GetData ();
        int itemOffset = e.NumberOfItems;
        int endOffset = Math.Min(itemOffset + _itemsPerRequest, data.Count);
        e.EndOfItems = endOffset == data.Count;
 
        for (int i = itemOffset; i < endOffset; i++)
            cbTest.Items.Add(new RadComboBoxItem(data[i].ToString()));
 
    }
 
 
    private List<int> GetData()
    {
        List<int> results = new List<int>(50);
        for (int i = 1; i < 51; i++)
        {
            results.Add(i);
        }
 
        return results;
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Ivana
Telerik team
answered on 21 Dec 2011, 05:41 PM
Hi William,

I have made small modifications to your code. You could download the attached files and see if the  provided workaround works for your scenario.

The reason why the items loaded in Page_Load are loaded with every group of items that is requested, is that the load on demand mechanism loads the items on request and static items are not allowed.

There are two approaches that could be used to avoid this behavior:
  • Clear the items of the RadComboBox when you are populating the new group of items.
  • Don't populate the RadComboBox's items collection on Page_Load.
Using the second approach, the load on demand will be needed only once and if the users see the options they want to select, there will not click the "more" box at the bottom of the drop-down list, and no other post-backs to the server will be made.

I hope this is helpful.

All the best,
Ivana
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
wdudek
Top achievements
Rank 1
answered on 21 Dec 2011, 05:56 PM
Thanks, I thought I had tried that and it didn't seem to work. But regardless your example code did exactly what I was looking for.

Bill
Tags
ComboBox
Asked by
wdudek
Top achievements
Rank 1
Answers by
Ivana
Telerik team
wdudek
Top achievements
Rank 1
Share this question
or