Searching a ListBox or DropDownList using the ListSearchExtender Control using ASP.NET AJAX
Posted by: Suprotim Agarwal ,
on 2/16/2008,
in
Category ASP.NET AJAX
Abstract: The ListSearchExtender control is a control that ships with the Microsoft AJAX Control Toolkit. ListSearchExtender enables the DropDownList and ListBox to be searchable. The user can navigate to and select an item in the list by simply typing the first few characters. This could be especially useful if there is a long list to be searched.
Searching a ListBox or DropDownList using the ListSearchExtender Control using ASP.NET AJAX
The ListSearchExtender control is a control that ships with the Microsoft AJAX Control Toolkit. ListSearchExtender enables the DropDownList and ListBox to be searchable. The user can navigate to and select an item in the list by simply typing the first few characters. This could be especially useful if there is a long list to be searched.
Let us quickly see how to use the ListSearchExtender control in our projects.
Step 1: Add a ListBox and DropDownList to the webform.
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
Click on the
ListBox and type the word to search<br />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<br />
<br />
<br />
Click on the
DropDownList and type the word to search<br />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<br />
</div>
</form>
</body>
Step 2: Populate the ListBox and DropDownList with a few items. Over here, we are using the ‘for’ loop to add items to the ListBox and DropDownList. If you want, you can implement a real world scenario where you can connect to a database and bind the controls to a collection.
C#
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
ListBox1.Items.Add("Items " + i.ToString());
DropDownList1.Items.Add("Items " + i.ToString());
}
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = 0 To 99
ListBox1.Items.Add("Items " & i.ToString())
DropDownList1.Items.Add("Items " & i.ToString())
Next i
End Sub
Step 3: Drag and drop two ’ ListSearchExtender’ controls to the form. If you have the AJAX Control Toolkit, you will find the ListSearchExtender control in the ‘Ajax Toolkit’ tab of the toolbox. Set the ‘TargetControlID’ of ListSearchExtender1 as ‘ListBox1’ and ‘TargetControlID’ of ListSearchExtender2 as ‘DropDownList1’ as shown below:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
Click on the
ListBox and type the word to search<br />
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<br />
<cc1:ListSearchExtender ID="ListSearchExtender1" runat="server" TargetControlID="ListBox1">
</cc1:ListSearchExtender>
<br />
<br />
Click on the
DropDownList and type the word to search<br />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<cc1:ListSearchExtender ID="ListSearchExtender2" runat="server" TargetControlID="DropDownList1">
</cc1:ListSearchExtender>
<br />
</div>
</form>
</body>
Run the application and click on the ListBox control. Search for an item (Eg: “Items 45”). You will observe that the ListSearchExtender searches the item and highlights it for us. Similarly try it out for the dropdownlist control too.
Simple ain’t it!! We will explore some other important controls in the articles to come. I would encourage you to do the same. If you have an article to share for dotnetcurry, submit it over here. I hope you found this article useful and I thank you for viewing it.
If you liked the article, please subscribe to my RSS feed over here.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn