A DropDownList is also commonly known as combo box. It can contain multiple data members, but unlike a normal list box the users can choose only one value from this control. Though the functionality of this DropDownList is much like a Single Row Select List Box, a DropDownList can save a lot of GUI space as it is rendered on a Single line and is expanded only when the user clicks on the Control.
This DropDownList is provided as a Server Control in ASP .Net like many other controls. This DropDownList can be used to add data manually or even for dynamic binding with data base.
Declaring a DropDownList in ASP .Net:
A simple declaration for DropDownList can be done as below.
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
The above is a very simple declaration for a DropDownList. If we want to add some items statically it can be added inside the Page as follows.
<asp:DropDownList id="DropDownList1" runat="server"> <asp:ListItem Value="TestData1">TestData1</asp:ListItem> <asp:ListItem Value="TestData2">TestData2</asp:ListItem> </asp:DropDownList>
The above data can be added manually inside the ASPX page. Otherwise it can also be added by using the Properties –> Items in Visual Studio .Net. When this Items property is clicked, a button will be displayed. When this button is clicked it will prompt a dialog box, where the data can be entered.
Adding the Data using Data Binding to a DropDownList in ASP .Net :
Data binding on a control can be applied from various data sources. The data can come from a database, a .XML file, an array etc., Adding data from a Data from an Array is dealt nicely in a sample from the gotdotnet site. As there is no need to duplicate an already available resource, the following Para explains how to do Data binding with Database data.
The ADO .Net Data structure which can be supplied as the DataSource to the dropdownlist in ASP .Net is the DataReader class. This class is available as SqlDataReader in side the System.Data.SqlClient namespace and as OleDbDataReader inside the System.Data.OleDb namespace. Any of the above classes can be used depending on the data source. This article has been written with an SQL Server 2000 as the back end Database.
The code snippet for pulling out data from a database into a data reader is as follows.
SqlDataReader ddDR = null; SqlConnection ddSqlConnection = new SqlConnection("server=(local);database=[dbname];user id=username;password=password;connection reset=false;connection lifetime=5;min pool size=1;max pool size=50;"); SqlCommand ddSqlCommand = new SqlCommand("SELECT * FROM TableName", ddSqlConnection); ddSqlConnection.Open(); ddDR = ddSqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
The above code will pull the data from the table and keep it in the SqlDataReader. This can be used as follows to load the DropDownList in our asp .net application.
Reading the Data from a DropDownList in ASP .Net:
Reading the data from a DropDownList can be achieved in a very easy manner. The Items collection, which is a member of the DropDownList class holds all the details about the members. The values can be retrieved as DropDownList.Items[index] – replace the index with a number, which can go up to the maximum number of data members. This will return the value.
If one wants to find the selected value, then the DropDownList.SelectedItem.Value can be used. The SelectedItem is a member of the class.
For removing the items, the function DropDownList.Remove() can be used. To delete all the items from the DropDownList in ASP .Net, the DropDownList.Items.Clear() function can be used.
DropDownList1.DataSource = ddDR; DropDownList1.DataTextField = "DisplayValueFieldFromDB"; DropDownList1.DataValueField = "DataValueFieldFromDB"; DropDownList1.DataBind();
The above code will load the DropDownList with the Display Value as DisplayValueFieldFromDB and Data Value as DataValueFieldFromDB. The following zip file contain the code sample for the above.