Introduction:

Here we will explain how to use DropDownList in asp.net or what is DropDownList in asp.net or how to get DropDownList selected items in asp.net with example.

Description:

  • The DropDownList control is used to create a drop-down list. It’s allowed to single select option to the user from multiple listed items. You can specify its height and width in pixel by setting its height and width but you will not be able give mutliple select option to the user from multiple listed items. When it is rendered on the page, it is implemented through <select/> HTML tag. It is also called as Combo box.

The DropDownList control support data binding. To bind the control to a data source, create a data source, such as a System.Collections.ArrayList object, that contains the items to display in the control. Then, use the Control. DataBind method to bind the data source to the DropDownList control.

Use the SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control. The index can then be used to retrieve the selected item from the Items collection of the control.

  • Example:

DropDownList.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownList.aspx.cs" Inherits="AspDotNet.DropDownList" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div><b>DropDownList Demo</b></div>
            <table>
                <tr>
                    <td>Select as Option:</td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"
                            Height="28px" Width="100px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td colspan="2"><b>Selected Item: </b></td>
                </tr>
                <tr>
                    <td>Item Value: </td>
                    <td>
                        <asp:Label ID="itemvalue" runat="server" Text="Label"></asp:Label></td>
                </tr>
                <tr>
                    <td>Item Text: </td>
                    <td>
                        <asp:Label ID="itemtext" runat="server" Text="Label"></asp:Label></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 

DropDownList.aspx.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class DropDownList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList list = new ArrayList();
                list.Add(new ListItem("Item1", "I001"));
                list.Add(new ListItem("Item2", "I002"));
                list.Add(new ListItem("Item3", "I003"));
                list.Add(new ListItem("Item4", "I004"));
                list.Add(new ListItem("Item5", "I005"));
 
                DropDownList1.DataSource =list;
                DropDownList1.DataTextField = "Text";
                DropDownList1.DataValueField = "Value";
                DropDownList1.DataBind();
            }
        }
 
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            itemtext.Text =DropDownList1.SelectedItem.Text;
            itemvalue.Text =DropDownList1.SelectedItem.Value;
        }
    }
}

Above example when you change selected item then called SelectedIndexChanged event where you can get selected value and text.

Let’s see output:

Use DropDownList Control in ASP.NET

Following are some important DropDownList  properties that are very useful.

 

SelectedValue

Get the value of the Selected item from the dropdown box.

SelectedIndex

Gets or Sets the index of the selected item in the dropdown box.

SelectedItem

Gets the selected item from the list.

Items

Gets the collection of items from the dropdown box.

DataTextField

Name of the data source field to supply the text of the items. (No need to set when you are adding items directly into .aspx page.)

DataValueField

Name of the data source field to supply the value of the items. (No need to set when you are adding items directly into .aspx page.)

DataSourceID

ID of the datasource component to provide data. (Only used when you have any DataSource component on the page, like SqlDataSource, AccessDataSource etc.)

DataSource

The datasource that populates the items in the dropdown box. (Generally used when you are dynamically generating the items from Database.)

AutoPostBack

true or false. If true, the form is automatically posted back to the server when user changes the dropdown list selection. It will also fire OnSelectedIndexChanged method.

AppendDataBoundItems

true or false. If true, the statically added item (added from .aspx page) is maintained when adding items dynamically (from code behind file) or items are cleared.

OnSelectedIndexChanged

Method name that fires when user changes the selection of the dropdown box. (Fires only when AutoPostBack=true.)

 

 

I hope this will help to you.



0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like