Introduction to Drop Down List in ASP.NET
We all must have seen various web pages, which are having drop-down which consist of various options .it is usually a list with options. Mostly in the registration forms or while entering our details while signing on any page, we fill our details, there we can see the drop-down list with countries, states, area, etc. Dropdown List control is usually used by the developer to give a chance to select one option out of multiple options given in the drop-down list or listed items. In this topic, we are going to learn about the drop down list in asp.net. It is used to store multiple items. Dropdown list control is also named as Combo box control.
<asp:DropDownList ID=“Trainings” runat=“server”>
<asp:ListItem Enabled=”true” Text= “Select Subject” Value= “-1”></asp:ListItem>
<asp:ListItem Text= “Science” Value=”1”><asp:ListItem>
< asp:ListItem Text= “Politics” Value=”2”><asp:ListItem>
Properties of Drop Down List in ASP.NET
Let us see some of the important properties of drop down list in ASP .NET
- DropDownList1.Items.Count: it is used to provide the total number of options or items in the drop-down list.
- DropDownList1.Items.Add(“ItemName”): suppose if we want to add some new item, then this property is useful for adding the item in the drop-down list.
- DropDownList1.Items.Remove(“ItemName”): It will help to remove the item from the drop-down list.
- DropDownList1.Items.Insert(int index, “ItemName”): if we want the item to be added at a specific position, this property helps to add a new item at a specific position in the drop-down list control.
- DropDownList1.Items.RemoveAt(int index): it will remove the specific item from specific position (index) from the drop down list control.
- DropDownList1.Items.Clear(): if we don’t want all the items and now we want to add another or maybe we want to change the items from the drop-down list.so it’s better to clear all the items first. This property is used to clear all the provided items from the drop-down list.
- DropDownList1.SelectionItem.Text: this is one of the important properties because it will return the text value which is in the selected items in the drop-down list.
- DropDownList1.SelectedIndex: index will always start from zero. When we select any item from the drop-down list, it is associated with the index. This property will return the position of the selected item that is its index value.
- DropDownList1.DataSource: it is mostly the DataTable or DataSet.
- DropDownList1.DataValueField: It will bind the value to the drop-down list which will be visible in the dropdown list.
- Item: it will provide the collection of the items from the drop-down list.
- AutoPostBack: Its value is ‘true’ or ‘false’. True represents that form is posted back automatically to the server when the user changes the dropdown list selection.
- DataTextField: values will be visible by the end-users.it is used to set the text in the Dropdown list control.
- DataValueFeild: This is used to set the name of the column as a value in the drop-down list. This value is not visible by the end-user.
How to create a Dropdown List in ASP.Net?
Let us see now how can we create a Dropdown list in ASP .NET with the help of below steps.
Open Visual Studio 2015 and create a new webform.
Now we need to drag the drop-down list from the toolbox and drop it in the form.
After dragging the dropdownlist it will look like this
For adding the items in the list, go to the Items property and add it.
Click on the items (collection) and it will pop up a new window as given below. Initially, it does not have any item. It provides an add button to add new items to the list.
After clicking on items, it will give us a new window pop up. In this window initially, there will not be any items listed. We need to click on the add button to add new items on the list.
Provide the values to the text and value properties, add the items.
We can also provide its height and width by setting it via properties. It has properties by which we can modify the back color, forecolor, etc. through style properties of <span>. Like image control property it doesn’t have any borderstyle, borderwidth properties in the drop-down list control. It has very fewer properties to enhance as compared to other controls.
We have two options to add the items, one is to add its multiple options in the items list directly writing into .aspx page. Secondly, at run time we can add it dynamically or bind via the database.
Example of Drop Down List in ASP.NET
Now we will see how we implement drop down list in asp.net via code. Let’s have a look at this example for better understanding.
In this example, we want to display color from the multiple options of color present in the drop-down list.
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="DropDownListExample._Default" %>
<form id="form1" runat="server">
<p>Select a color to start</p>
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit"> <br />
<asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
public partial class _Default : Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
if (DropDownList1.SelectedValue == "")
Label1.Text = "";
Label1.Text = "Your selected color is: " + DropDownList1.SelectedValue;
After executing the above code, we will get output with dropdown having different colors in it.
DropDownList1.SelectedValue property is used in this code to provide the selected item from the drop-down. Here, ListItem contains all the required list of item, which needs to show in the drop-down list.
After selecting the color from the drop-down list in asp.net, it will be displayed to the user. Here we selected “Black” Color.
I hope now you have some basic understanding of the Drop Down List in ASP.NET. Therefore, we have seen that how the drop-down list used in .net framework to display it on the various forms and registration pages.it is a very useful feature when you have a huge list so it is better to store these values in the drop-down list.
This is a guide to Drop Down List in ASP.NET. Here we discuss how to create a Drop down list in ASP.NET along with the example and some essential properties. You may also have a look at the following articles to learn more –