In this article I will explain how to bind images to dropdownlist in asp.net using JQuery.
In previous articles I explained about how to implement cascading dropdownlist and Ajax Cascading dropdownlist. Now I will explain how to bind dropdownlist with images dynamically in asp.net using JQuery. In one of website I saw one dropdownlist that contains Country names along with country flags. I feel it’s better to write post to explain how to bind images to dropdownlist in asp.net. To implement this concept by using JQuery plugin we can achieve this easily.
Column Name | Data Type | Allow Nulls |
ID | int(set identity property=true) | No |
CountryName | varchar(50) | Yes |
CountryImage | varchar(50) | Yes |
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Dropdownlist with Images</title> <link rel="stylesheet" type="text/css" href="msdropdown/dd.css" /> <script type="text/javascript" src="msdropdown/js/jquery-1.6.1.min.js"></script> <script type="text/javascript" src="msdropdown/js/jquery.dd.js"></script> <!-- Script is used to call the JQuery for dropdown --> <script type="text/javascript" language="javascript"> $(document).ready(function(e) { try { $("#ddlCountry").msDropDown(); } catch (e) { alert(e.message); } }); </script> </head> <body> <form id="form1" runat="server"> <table> <tr> <td align="right"> <b>Country:</b> </td> <td> <asp:DropDownList ID="ddlCountry" runat="server" Width="150px" onselectedindexchanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> </td> </tr> <tr> <td> <b>Seelcted Country:</b> </td> <td> <asp:Label ID="lbltext" runat="server"></asp:Label> </td> </tr> </table> </form> </body> </html> |
using System; using System.Data; using System.Data.SqlClient; using System.Web.UI.WebControls; |
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDropDownList(); BindTitles(); lbltext.Text = ddlCountry.Items[0].Text; } } /// <summary> /// This Method is used to bind titles to each element of dropdown /// </summary> protected void BindTitles() { if (ddlCountry != null) { foreach (ListItem li in ddlCountry.Items) { li.Attributes["title"] = "Images/" + li.Value; // setting text value of item as tooltip } } } /// <summary> /// Bind Dropdownlist Data /// </summary> protected void BindDropDownList() { SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true"); con.Open(); SqlCommand cmd = new SqlCommand("select * from CountryImages", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); ddlCountry.DataTextField = "CountryName"; ddlCountry.DataValueField = "CountryImage"; ddlCountry.DataSource = ds; ddlCountry.DataBind(); con.Close(); } protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { lbltext.Text = ddlCountry.SelectedItem.Text; BindTitles(); } |
Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI.WebControls Partial Class VbSample Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then BindDropDownList() BindTitles() lbltext.Text = ddlCountry.Items(0).Text End If End Sub ''' <summary> ''' This Method is used to bind titles to each element of dropdown ''' </summary> Protected Sub BindTitles() If ddlCountry IsNot Nothing Then For Each li As ListItem In ddlCountry.Items ' setting text value of item as tooltip li.Attributes("title") = "Images/" & li.Value Next End If End Sub ''' <summary> ''' Bind Dropdownlist Data ''' </summary> Protected Sub BindDropDownList() Dim con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true") con.Open() Dim cmd As New SqlCommand("select * from CountryImages", con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() da.Fill(ds) ddlCountry.DataTextField = "CountryName" ddlCountry.DataValueField = "CountryImage" ddlCountry.DataSource = ds ddlCountry.DataBind() con.Close() End Sub Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) lbltext.Text = ddlCountry.SelectedItem.Text BindTitles() End Sub End Class |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
|



Subscribe by RSS
Subscribe by Email
29 comments :
super interesting. Thanks for sharing
really nice suresh :), Im also suresh ;)
Hi Mr. Suresh ,
I use the custom dropdown v2.37.5 as the following:
1. I have a table with name COUNTRY with columns [id,countryName,flag].
2. I have a table with name MFG with columns [id,mfgName,countryID].
3.I fill the Dropdown as the following:
[Select]
[Germany]
VW
Mercedes
BMW
[Korea]
Hyundai
KIA
and so on.
What I need to do is how to make the countries items with background color?
thank you.
Very very helpful! Really saved my time of work!
Thak you very much:)
Alona (Ukraine)
hi this is deepak garg,
i have gone through many blogs but your blog has really some different tutorials.
now i have decided to read your blog regularly.
pls if u can then also start tutorial which tell how exactly a project should be started with proper SDLC process. that will really help me .......
this is my email id deepagargak@gmail.com
thanks
After running this code on re sizing an image after upload and still retaining the same quality, i got this error.
error 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream'
Validation Complete
After running this code on resizing an image after upload i got this error.
error 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream'
Validation Complete
Hi!
really nice article but i am facing problem when using update panel Is there any solution?
there is no single line of code to bind images.it is not at all working. it is a wrong post.
@ Comment 9...
Before give wrong comment check the code i am binding images in code behind. Check it once if it not works means how can i show the demo. Please check thoroughly before give wrong comments...
man you r d best............
thnk u...
Does it work with Masterpages?
yeah it will work in masterpages also...
Hi suresh iam using a increment of rows in the html page using javascript with row elements(checkbox,dropdownlist,textarea)in each row
the problem is how can i bind data from sql server so that dropdownlist of each row should fetch data from database...
awesome. i need a code to implement the SQL database data with multiple sheets in same excel sheet in c# .net. will you help me
after executing it shows the msg that "" $("#ddl_Operator").msDropDown() "" is not a function
plzz help me to solve this problem !!!!
is there a way to set value for the dropdown box?
"after executing it shows the msg that "" $("#ddl_Operator").msDropDown() "" is not a function"
"#ddl_Operator" y0u should replace it with the dropdown ID, i guess that helps
after executing it shows the msg that "" $("#ddl_Operator").msDropDown() "" is not a function
help me sir
country change page refresh.
i use dropdown list
in asp update panel flag image not retrive....
any suggestion how to stop page reload.....
absolutely working great work thank you..
Hey When I Created a Insert Option For This Dropdown and allowed user to insert some images with names in to The dropdown the dropdown certainly braking in to two after inserting. and when i refresh then again it is coming in to one is their any solution for this problem.
Its very userful & interesting article!!
your code is awesome. It worked for me.
Now i want to remove scroll from dropdown.
Please help me out.. Thanks in advance
This code not work in IE browser. is ther any solution for it. Please help me out. Thanks in advance.
Is is possible to apply this code for more than 2 dropdown??