Descending DropDown List In ASP.NET

Introduction

A Dropdown list is a combination box that contains a list of multiple items. The user may select an item from the menu by dragging the mouse from the menu title or by clicking the title and then clicking the item. The dropdown list box is read-only. A drop-down list is a list in which the selected item is always visible, and the others are visible on demand by clicking a drop-down button.

In this article I am describing a descending dropdown list in ASP.NET.

Complete Program

DropDownDemo.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

public partial class DropDownDemo : System.Web.UI.Page

{

    private string strconnection = "Data Source=MCNDESKTOP27;Initial Catalog=MysimpleDB;Persist Security Info=True;User ID=sa;Password=**********";

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            Binddropdown();

        }

    }

    protected void Binddropdown()

    {

        SqlConnection strcon = new SqlConnection(strconnection);

        strcon.Open();

        SqlCommand cmmd = new SqlCommand("select * from CountryTable", strcon);

        SqlDataAdapter da = new SqlDataAdapter(cmmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        strcon.Close();

        ddlCountry.DataSource = ds;

        ddlCountry.DataTextField = "CountryName";

        ddlCountry.DataValueField = "CountryId";

        ddlCountry.DataBind();

        ddlCountry.Items.Insert(0,new ListItem("--select--","0"));

        ddlState.Items.Insert(0, new ListItem("--select--", "0"));

        ddlRegion.Items.Insert(0, new ListItem("--select--", "0"));

    }

    protected void ddlCountry_IndexChanged(object sender, EventArgs e)

    {

        int CountryId = Convert.ToInt32(ddlCountry.SelectedValue);

        SqlConnection strcon = new SqlConnection(strconnection);

        strcon.Open();

        SqlCommand cmmd = new SqlCommand("select * from StateTable where CountryId=" + CountryId, strcon);

        SqlDataAdapter da = new SqlDataAdapter(cmmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        strcon.Close();

        ddlState.DataSource = ds;

        ddlState.DataTextField = "StateName";

        ddlState.DataValueField = "StateId";

        ddlState.DataBind();

        ddlState.Items.Insert(0, new ListItem("--select--", "0"));

        if (ddlState.SelectedValue == "0")

        {

            ddlRegion.Items.Clear();

            ddlRegion.Items.Insert(0, new ListItem("--select--", "0"));

        }

    }

    protected void ddlState_IndexChanged(object sender, EventArgs e)

    {

        int StateId = Convert.ToInt32(ddlState.SelectedValue);

        SqlConnection strcon = new SqlConnection(strconnection);

        SqlCommand cmmd = new SqlCommand("select * from RegionTable where StateId=" + StateId, strcon);

        SqlDataAdapter da = new SqlDataAdapter(cmmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        strcon.Close();

        ddlRegion.DataSource = ds;

        ddlRegion.DataTextField = "RegionName";

        ddlRegion.DataValueField = "RegionId";

        ddlRegion.DataBind();

        ddlRegion.Items.Insert(0,new ListItem("--select--","0"));

    }

}

 

DropDownDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownDemo.aspx.cs" Inherits="DropDownDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style type="text/css">

        .auto-style1

        {

            font-size: large;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0);

        font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal;

        font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto;

        text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;

        -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px;

        -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto;

        -webkit-text-stroke-width: 0px; "><span class="auto-style1" style="color: rgb(34, 34, 34);

        font-family: Verdana, sans-serif; text-align: left; "><strong>Descending Dropdown Sample</strong></span>

        </span><br />

        <br />

        <asp:Label ID="Label1" runat="server" Text="Select Country" style="z-index: 1; left: 10px; top: 73px; position: absolute"></asp:Label>

&nbsp;&nbsp;

        <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_IndexChanged"

            style="z-index: 1; left: 138px; top: 65px; position: absolute">

        </asp:DropDownList>

        <br /> 

    </div>

        <asp:Label ID="Label2" runat="server" Text="Select State" style="z-index: 1; left: 9px; top: 105px; position: absolute"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlState_IndexChanged"

            style="z-index: 1; left: 139px; top: 102px; position: absolute">

        </asp:DropDownList>

        <br />

        <asp:Label ID="Label3" runat="server" Text="Select Region" style="z-index: 1; left: 6px; top: 140px; position: absolute"></asp:Label>

&nbsp;&nbsp;&nbsp;<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:DropDownList ID="ddlRegion" runat="server" style="z-index: 1; left: 139px; top: 140px; position: absolute">

        </asp:DropDownList>

    </form>

</body>

</html>

 

Output


Animation1.gif

For more information, download the attached sample application.

Up Next
    Ebook Download
    View all
    Learn
    View all