Binding Image In DropDownList In ASP.NET

In this article you will learn how to bind Image in DropDownList in ASP.NET.

Binding image with DropDownList is an important concept while we are working in project. This mechanism  is not achieved by simple dropdownlist. We have to add some  additional jQuery to  get this thing done. Here in the following link you can download the required jQuery.

Download mirrors for JQuery.dd.js

Create a new ASP.NET project. Add a new web form in it as I have shown in the following screenshot. 



Here, I have downloaded the jQuery from above link and added to my project.

This contain some CSS files and some jQuery files.

Now in my web form I have added one ASP.NET DropDownList as follows.

 
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div> </div>  
  4.         <asp:DropDownList ID="DropDownList1" runat="server" Width="200px" Height="16px"> </asp:DropDownList>  
  5.     </form>  
  6. </body>  
Now here is my database table where, I get my country name.

 

Here is my code to bind the country and url to DropDownList.
  1. using System.Data;  
  2. using System.Data.SqlClient;  
  3. public partial class _Default: System.Web.UI.Page  
  4. {  
  5.     SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123");  
  6.     protected void Page_Load(object sender, EventArgs e)  
  7.     {  
  8.         if(!IsPostBack)  
  9.         {  
  10.             data();  
  11.             BindTitle();  
  12.         }  
  13.     }  
  14.     public void data()  
  15.     {  
  16.         SqlDataAdapter da = new SqlDataAdapter("select * from Details", con);  
  17.         DataTable dt = new DataTable();  
  18.         da.Fill(dt);  
  19.         DropDownList1.DataSource = dt;  
  20.         DropDownList1.DataTextField = "name";  
  21.         DropDownList1.DataValueField = "url";  
  22.         DropDownList1.DataBind();  
  23.     }  
  24. }  
  25. Now here is my BindTitle() method.  
  26. protected void BindTitle()  
  27. {  
  28.     if(DropDownList1 != null)  
  29.     {  
  30.         foreach(ListItem li in DropDownList1.Items)  
  31.         {  
  32.             li.Attributes["title"] = li.Value;  
  33.         }  
  34.     }  
  35. }  
Now here I have added the jQuery and CSS in the head section in default.aspx page.
  1. <head runat="server">  
  2.     <title></title>  
  3.     <link href="msdropdown/dd.css" rel="stylesheet" />  
  4.     <script src="msdropdown/js/jquery-1.6.1.min.js"></script>  
  5.     <script src="msdropdown/js/jquery.dd.js"></script>  
  6.     <script type="text/javascript">  
  7.     $(document)  
  8.         .ready(function (e)  
  9.         {  
  10.             try  
  11.             {  
  12.                 $("#DropDownList1")  
  13.                     .msDropDown();  
  14.             }  
  15.             catch(e)  
  16.             {  
  17.                 alert(e.message);  
  18.             }  
  19.         });  
  20.     </script>  
  21. </head>  
Now save and run the project you will get the following output.

 
So in this way you can bind Images in DropDownList.