Sunday, 7 September 2014

DropdownList Item With Custom Icon Image In Asp.net Using C#.Net

9/07/2014 - By Pranav Singh 8

In this article I will show you how you can display image into dropdownlist or select list option in asp.net using c#.net . This article will display the select list option item with image.


So for this article first we will create a new asp.net application and add dropdown list into you page. After adding dropdownlist your .aspx page code will look as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ddlitemwithicon.aspx.cs"
    Inherits="ProjectDemo_Asp.et.ddlitemwithicon" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>DropdownList Item With Custom Icon Images In Asp.net Using C#.Net</title>
    <style>
        .ddlstyle
        {
            width: 200px;
        }
        option
        {
            padding-left: 20px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

In above code I have used a style sheet to provide padding or space between the text and the image which going to display with option.

Now let’s check the code for binding and displaying the options in dropdown.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ProjectDemo_Asp.et
{
    public partial class ddlitemwithicon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // if (!IsPostBack)
            {
                DataTable objdt = new DataTable();
                objdt = GetDataForChart();
                DropDownList1.DataSource = objdt;
                DropDownList1.DataTextField = "Country";
                DropDownList1.DataValueField = "Id";
                DropDownList1.DataBind();
                //Now add class to each and every item in dropdown
                //Now, add a "SysCode" attribute to each item in the dropdown list
                string imageURL = "";
                for (int i = 0; i < DropDownList1.Items.Count; i++)
                {
                    switch (DropDownList1.Items[i].Text)
                    {
                        case "India": imageURL = "Images/flag-of-India.png";
                            break;
                        case "Kuwait": imageURL = "Images/flag-of-Kuwait.png";
                            break;
                        case "Egypt": imageURL = "Images/flag-of-Egypt.png";
                            break;
                        case "Bangladesh": imageURL = "Images/flag-of-Bangladesh.png";
                            break;
                        case "Afghanistan": imageURL = "Images/flag-of-Afghanistan.png";
                            break;
                    }
                    ListItem item = DropDownList1.Items[i];
                    item.Attributes["style"] = "background: url(" + imageURL + ");background-repeat:no-repeat;";
                }
            }
        }
        /// This method will provide data
        /// In this methos you can fatch data from DB and pass it to chart control
        ///
        public DataTable GetDataForChart()
        {
            DataTable _objdt = new DataTable();

            _objdt.Columns.Add("Country", typeof(string));
            _objdt.Columns.Add("Id", typeof(long));

            _objdt.Columns.Add("LabelValue");
            var _objrow = _objdt.NewRow();
            _objrow["Country"] = "India";
            _objrow["Id"] = 1;

            _objdt.Rows.Add(_objrow);
            _objrow = _objdt.NewRow();
            _objrow["Country"] = "Kuwait";
            _objrow["Id"] = 2;
            _objdt.Rows.Add(_objrow);

            _objrow = _objdt.NewRow();
            _objrow["Country"] = "Egypt";
            _objrow["Id"] = 3;
            _objdt.Rows.Add(_objrow);

            _objrow = _objdt.NewRow();
            _objrow["Country"] = "Bangladesh";
            _objrow["Id"] = 4;
            _objdt.Rows.Add(_objrow);

            _objrow = _objdt.NewRow();
            _objrow["Country"] = "Afghanistan";
            _objrow["Id"] = 5;
            _objdt.Rows.Add(_objrow);
            return _objdt;
        }
    }
}

Here in above code I have prepared static data into datatable. You can use db table data to display the records.

Now just check the page load section. In this first I have bind the dropdown list item and then on the bases of text I have selected image and pass it to list item to display. Now I have added attribute to display image with option in dropdown list.

Now we have done just check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

8 comments:

  1. not working in chrome browser

    ReplyDelete
  2. Not working on edge either

    ReplyDelete
    Replies
    1. did you found any solution for this ?

      Delete
    2. fk u not working chrome ie opera

      Delete
    3. Control your vocabulary

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Dear Sir,
    Not working in google chrome ,IE and Opera
    kindly fix this issue .. i tried lot but not getting through. My email id :sandipshaw7@gmail.com.

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top