Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
select

ComboBox / LinqDataSource

  • select

  • This example demonstrates how to bind RadComboBox to LinqDataSource and Linq to SQL data classes.

    The first combobox is bound declaratively to the LinqDataSource control. Filtering is performed using the Where property of the data source control:

    protected void RadComboBox1_OnItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
    	LinqDataSource1.Where = "ContactName.StartsWith(\"" + e.Text + "\")";
    	RadComboBox1.DataBind();
    }
    

    The second combobox demonstrates how to implement paging on the database using LINQ:

    protected void RadComboBox2_OnItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
    	NorthwindDataContext context = new NorthwindDataContext();
    
    	var allCustomers = from customer in context.Customers
    					   orderby customer.ContactName
    					   select customer;
    	
    	if (!String.IsNullOrEmpty(e.Text))
    	{
    		allCustomers = from customer in context.Customers
    					   where customer.ContactName.StartsWith(e.Text)
    					   orderby customer.ContactName
    					   select customer;
    	}
    
    	var customers = allCustomers.Skip(e.NumberOfItems).Take(10);
    	RadComboBox2.DataSource = customers;
    	RadComboBox2.DataBind();
    
    	int endOffset = e.NumberOfItems + customers.Count();
    	int totalCount = allCustomers.Count();
    
    	if (endOffset == totalCount)
    		e.EndOfItems = true;
    
    
    	e.Message = String.Format("Items 1-{0} out of {1}", endOffset, totalCount);
    }
    

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="ComboBox_Examples_PopulatingWithData_LinqDataSource_DefaultCS" %>

    <%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="qsf" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <qsf:HeadTag ID="Headtag1" runat="server"></qsf:HeadTag>
        <style type="text/css">
            .rcbHeader ul, .rcbFooter ul, .rcbItem ul, .rcbHovered ul, .rcbDisabled ul
            {
                width: 100%;
                display: inline-block;
                margin: 0;
                padding: 0;
                list-style-type: none;
            }
            .rcbHeader ul:after, .rcbFooter ul:after, .rcbItem ul:after, .rcbHovered ul:after, .rcbDisabled ul:after
            {
                content: ".";
                display: block;
                visibility: hidden;
                font-size: 0;
                line-height: 0;
                height: 0;
                clear: both;
            }
            .col1, .col2, .col3
            {
                float: left;
                width: 120px;
                margin: 0;
                padding: 0 5px 0 0;
                line-height: 14px;
            }
            label.text
            {
                font: 13px 'Segoe UI' , Arial, sans-serif;
                color: #4888a2;
                padding: 6px 18px 0 0;
                display: inline-block;
                clear: both;
                width: 150px;
            }
            .combo-list
            {
                list-style: none;
                padding: 0;
                margin: 0;
            }
        </style>
    </head>
    <body class="BODY">
        <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
            <qsf:Header ID="Header1" runat="server" NavigationLanguage="C#"></qsf:Header>
            <ul class="combo-list">
                <li>
                    <label class="text">
                        Multi-column combobox:
                    </label>
                    <telerik:RadComboBox runat="server" ID="RadComboBox1" DataTextField="ContactName"
                        EnableLoadOnDemand="True" DataSourceID="LinqDataSource1" OnItemsRequested="RadComboBox1_ItemsRequested"
                        Width="420px" Height="190px" HighlightTemplatedItems="true">
                        <HeaderTemplate>
                            <ul>
                                <li class="col1">Contact Name</li>
                                <li class="col2">City</li>
                                <li class="col3">Title</li>
                            </ul>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <ul>
                                <li class="col1">
                                    <%# Eval("ContactName") %></li>
                                <li class="col2">
                                    <%# Eval("City") %></li>
                                <li class="col3">
                                    <%# Eval("ContactTitle") %></li>
                            </ul>
                        </ItemTemplate>
                    </telerik:RadComboBox>
                </li>
                <li>
                    <label class="text">
                        Paged combobox:
                    </label>
                    <telerik:RadComboBox runat="server" ID="RadComboBox2" DataTextField="ContactName"
                        EnableLoadOnDemand="True" OnItemsRequested="RadComboBox2_ItemsRequested" Height="100px"
                        ShowMoreResultsBox="true" EmptyMessage="Type here ...">
                    </telerik:RadComboBox>
                </li>
            </ul>
    <asp:LinqDataSource runat="server" ID="LinqDataSource1" ContextTypeName="LinqToSqlReadOnly.NorthwindReadOnlyDataContext"
    OrderBy="ContactName" Select="new (ContactName, City, ContactTitle)" TableName="Customers">

            </asp:LinqDataSource>
            <qsf:Footer ID="Footer1" runat="server"></qsf:Footer>
        </div>
        </form>
    </body>
    </html>

Get more than expected!

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2012 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451