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;
}
}
}