.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to bind Dictionary to List box and dropdown list

Posted By:shankey       Posted Date: February 10, 2011    Points: 75    Category: ASP.NET    URL: http://www.dotnetspark.com  

How to bind Dictionary to List box and dropdown list
 

Contents
How to bind Dictionary pair to List box and dropdown list
Step 1
Step 2
Step 3
Output:

 

How to bind Dictionary pair to List box and dropdown list

This tutorial will let you learn how to bind Dictionary pair to the listbox/dropdown list. Following are some simple steps to bind your listbox/dropdown list to Dictionary pair 

 

Step 1

Create a Dictionary pair in you code behind file i.e .CS file. Following code shows how the Dictionary pair of  is created. dStudent is a Dictionary pair which stores a list of student name as value with id as key.
///Creating Dictionary of for Student.
Dictionary<intstring> dStudent = new Dictionary<intstring>();
 
//Adding key value pair to the dictionary
 dStudent.Add(0, "Eena");
 dStudent.Add(1, "Meena");
 dStudent.Add(2, "Deeka");
 dStudent.Add(3, "Tom");
 dStudent.Add(4, "Dick");
 dStudent.Add(5, "Harry");
 dStudent.Add(6, "Yamla");
 dStudent.Add(7, "Pagla");
 dStudent.Add(8, "Dewana");
 dStudent.Add(9, "Guru");
 dStudent.Add(10, "Sholay");
 

Step 2

After creating Dictionary pair It's the time to bind Dictionary pair with your listbox/dropdown list. Following code binds to dropdown and listbox too.
 //binding to the list
 lst.DataTextField = "Value";
 lst.DataValueField = "Key";
 lst.DataSource = dStudent;
 lst.DataBind();
 
 //binding to the Dropdown
 drp.DataTextField = "Value";
 drp.DataValueField = "Key";
 drp.DataSource = dStudent;
 drp.DataBind();
 
 
 
 
 
 
 
 

Step 3

 
 
Some code written on the selected index change event of both listbox and dropdownlist control. Following code will just display the Text and the value of the selected item of the respective control.
 
 
protected void lst_SelectedIndexChanged(object sender, EventArgs e)
 {
 lbl.Text+= "
"
 + String.Format("Selected Text:{0} and Value:{1}", lst.SelectedItem.Text, lst.SelectedValue);
 }
 protected void drp_SelectedIndexChanged(object sender, EventArgs e)
 {
 lbl.Text+= "
"
 + String.Format("Selected Text:{0} and Value:{1}", drp.SelectedItem.Text, drp.SelectedValue);
 }
 
 
 
 
 
 
 
 
 
 

Output:

When you run your application, the Dictionary pair is binded to the listbox/dropdown list.

When you select from list box, look at the selected item text and value assigned to the label
2-10-2011 12-04-49 PM.png
When you select from dropdown list, look at the selected item text and value assigned to the label

 
 
When you select from list box, look at the selected item text and value assigned to the label

When you select from dropdown list, look at the selected item text and value assigned to the label

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend