New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Server Templates
You can define the ItemTemplate programmatically by assigning a new ITemplate instanceto the ItemTemplate property of the searchBox, as shown in the code below:
ASPNET
<telerik:RadSearchBox RenderMode="Lightweight" ID="RadSearchBox1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="ProductName"
DataKeyNames="ProductId, ProductName"
DataValueField="ProductId" EmptyMessage="empty message" MaxResultCount="10">
<DropDownSettings Height="240" Width="300" />
</telerik:RadSearchBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Products]"></asp:SqlDataSource>
C#
protected override void OnInit(EventArgs e)
{
RadSearchBox1.DropDownSettings.ItemTemplate = new SearchBoxDropDownItemTemplate();
base.OnInit(e);
}
class SearchBoxDropDownItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Label id = new Label();
id.ID = "idLabel";
id.CssClass = "idClass";
id.DataBinding += new EventHandler(id_DataBinding);
container.Controls.Add(id);
}
private void id_DataBinding(object sender, EventArgs e)
{
Label target = (Label)sender;
Telerik.Web.UI.SearchBox.DropDownItem item = (Telerik.Web.UI.SearchBox.DropDownItem)target.BindingContainer;
string id = Convert.ToString((int)DataBinder.Eval(item.DataItem, "ProductId"));
target.Text = id;
}
}