Telerik blogs

If you have been using RadComboBox frequently in your projects, you have probably written the following or very similar code many times:

protected void RadComboBox1_ItemsRequested(object sender, 
    RadComboBoxItemsRequestedEventArgs e)
{
    SqlDataAdapter adapter = new SqlDataAdapter(
        "SELECT * from Customers WHERE CompanyName LIKE @text + '%'",
        ConfigurationManager.ConnectionStrings["NorthwindConnectionString"]
            .ConnectionString);
    adapter.SelectCommand.Parameters.AddWithValue("@text", e.Text);
 
    DataTable data = new DataTable();
    adapter.Fill(data);
 
 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++)
    {
        RadComboBox1.Items.Add(new RadComboBoxItem(
            data.Rows[i]["CompanyName"].ToString(), 
            data.Rows[i]["CompanyName"].ToString()));
    }
 
 if (data.Rows.Count <= 0)
        e.Message = "No matches";
 else 
        e.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", 
            itemOffset, data.Rows.Count);
}

You may have encapsulated the control, inherited it or used other ways to avoid writing the annoying code more than once. In case you have not used the control or the feature, you will still need to write the code if you want to take advantage of Load On Demand mechanism.

Following Q1 2010 Beta you will no longer need this as RadComboBox will support completely automatic server-side Load On Demand. What this means is that you can enable the full Load On Demand capabilities of the control by only setting few properties.

 

Below are the three easy steps to enable the feature:

First, assign a data source to the RadComboBox:

<telerik:RadComboBox ID="RadComboBox1" runat="server" Height="200px" 
 DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductID"> 
</telerik:RadComboBox> 
<asp:SqlDataSource runat="server" ID="SqlDataSource1" 
 ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
 ProviderName="System.Data.SqlClient" 
 SelectCommand="SELECT ProductName, ProductID FROM Products ORDER By ProductName" /> 
 

Then turn automatic Load On Demand on by setting the EnableAutomaticLoadOnDemand property to true. Now the control loads all Items from the assigned data source when it opens for the first time and filters them with the text that is typed in its input. It does all of this automatically in memory.

<telerik:RadComboBox ID="RadComboBox1" runat="server" Height="200px" 
 DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductID" 
 EnableAutomaticLoadOnDemand="true"> 
</telerik:RadComboBox> 
 

And finally, if you have not set the ShowMoreResultsBox and EnableVirtualScrolling properties to true, you can do so to enable the respective features.

What remains is to tell the RadComboBox how many Items to load on each request by setting its ItemsPerRequest property to the desired value. The default is -1, which means that the control will load all Items at once.

<telerik:RadComboBox ID="RadComboBox1" runat="server" Height="200px" 
 DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductID" 
 EnableAutomaticLoadOnDemand="true" ShowMoreResultsBox="true" 
 EnableVirtualScrolling="true" ItemsPerRequest="10"> 
</telerik:RadComboBox> 
 

Now Load On Demand is working at it full speed and all of this without writing a single line of code.

 

In addition to this, the ItemsRequested event fires and in the respective event handler you can still:

  • Add/Remove Items
  • Get/Set the ‘Show More Results’ message via the event arguments (i.e. the Message property)
  • Get/Set the EndOfItems event argument to explicitly stop subsequent ‘show more results/virtual scrolling’ requests

 

What is more you can use Automatic Load On Demand with any kind of declarative data source, including .NET 3.5’s Linq and EntityDataSource. (When working with the latter RadComboBox uses simple optimization techniques to improve the server-side performance.)

In fact you can use all types of enumerable data sources with the DataSource property. In this case, it is important to note that you set the property on each postback. RadComboBox will automatically bind to the data source whenever a request for Items is made.

 

You can see live demos showing Automatic Load On Demand with SqlDataSource, LinqDataSource and EntityDataSource here:


About the Author

Iana Tsolova

is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.

Comments

Comments are disabled in preview mode.