We use this extender on binded Dropdownlist.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give a suitable name dropdownlist_demo.

Step 2: In Solution Explorer you will get your empty website. Add a web form SQL Database. Follow these steps:

For Web Form:

dropdownlist_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it dropdownlist_demo.aspx.

For SQL Server Database:

dropdownlist_demo (Your Empty Website) - Right Click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.

Database chamber

Step 3: Go to your Database [Database.mdf], we will create two tables - tbl_Data and tbl_save. Go to the database.mdf - Table and Add New table, Design your table like the following:

Table - tbl_data (Don’t forget to make ID - Identity Specification - Yes)

table design

Show table data

table

Design chamber

Step 4: Now open your dropdownlist_demo.aspx file, where we create our design for Dropdownlist in ASP.NET.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title></title>
  7. <style type="text/css">
  8. .style1 {
  9. width: 576px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ToolkitScriptManager>
  16. <div>
  17. <table style="width:100%;">
  18. <tr>
  19. <td> </td>
  20. <td class="style1"> </td>
  21. <td> </td>
  22. </tr>
  23. <tr>
  24. <td> </td>
  25. <td class="style1">
  26. <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" DataTextField="country" DataValueField="country" Height="33px" style="margin-left: 267px">
  27. <asp:ListItem>--Select Country--</asp:ListItem>
  28. </asp:DropDownList>
  29. <asp:ListSearchExtender ID="DropDownList1_ListSearchExtender" runat="server" Enabled="True" PromptPosition="Top" PromptText="Type to Search" TargetControlID="DropDownList1"> </asp:ListSearchExtender>
  30. </td>
  31. <td> </td>
  32. </tr>
  33. <tr>
  34. <td> </td>
  35. <td class="style1"> </td>
  36. <td> </td>
  37. </tr>
  38. </table>
  39. </div>
  40. </form>
  41. </body>
  42. </html>
Code chamber

Step 5: Open your dropdownlist_demo.aspx.cs and write some code so that our application starts working.

dropdownlist_demo.aspx.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!Page.IsPostBack)
  14. {
  15. refreshdata();
  16. }
  17. }
  18. public void refreshdata()
  19. {
  20. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  21. SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
  22. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  23. DataTable dt = new DataTable();
  24. sda.Fill(dt);
  25. DropDownList1.DataSource = dt;
  26. DropDownList1.DataBind();
  27. }
  28. }
Output chamber

Output

select country

Hope you liked this. Have a good day!
Thank you for reading.