Dropdownlist example in asp .net

This tutorial will explain how to use asp .net dropdownlist control. Dropdownlist control is the one of the most used controls in asp .net projects. You can find dropdownlist in visual studio’s standard controls group. Dropdownlist control allows user to select only single item at a time. 

 
Asp .net’s Dropdownlist control is equivalent to HTML selectbox control. This control is very useful when you have large collection of data and you want the user can only select a single item from list of values. E.g. You have collection of country names to choose one from.
 
Some of the techniques to insert data into dropdownlist:
 
1) Add static values using ListItems.
2) You can bind dropdownlist using Arrays.
3) You can bind dropdownlist using collections.
4) You can bind dropdownlist using Database.
5) You can bind dropdownlist using XML file.
6) Using Datatables and many more.
 
Simple example of dropdownlist:
 
 
     
     
     
     
     


 
Here dropdownlist contains 3 listitems.  Each listItem has two properties:
 
Text: The text assigned to this property is visible to users on webpage.
Text property can be accessed by this code: 
ddlTest.SelectedItem.Text; Here ddlText is the Id of Dropdownlist control.
 
Value: The text assigned to this property is not visible to user. How ever they can read text assigned to Value property of Listitem. 
Value property can be accessed by this code: 
ddlTest.SelectedItem.Value; Here ddlText is the Id of Dropdownlist control.
 
 
Lets create a sample application and learn how to access Text and Value of ListItem from the dropdownlist control.
 
Step1: Create a new asp .net webpage.
 
Step2: Create a dropdownlist control and insert some default values in it.
 

     
     
     
     
     

 
Step3: Enable AutoPostBack property of dropdownlist. 
 
   
 
AutoPostBack property is used to determine whether postback to the server automatically occurs when the user changes the list selection or not.
 
Step4: Create a OnSelectedIndexChanged event in dropdownlist.
 
   
 
This will create a custom handler for the ListItem selection event in your code behind page:.
 
 protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
    {
       
    }
 
Step5: Read Text and Value from Selected ListItem and Display in page:
 
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedText = ddlTest.SelectedItem.Text;
        string selectedValue = ddlTest.SelectedItem.Value;

        //--- Show results in page.
        Response.Write("Selected Text is " + selectedText + " and selected value is :" + selectedValue);
    }
 
For more information about Dropdownlist Properties and Methods you can visit:

Demo

Dropdownlist-example-tutorial-in-asp-net-codingfusion-anujkoundal

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
8 + 3 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice